How to start a program in Linux
In Linux, there are several ways to start a program.
- Typing the executable file path directly in the command line, for example:
/path/to/program
Alternatively, use the program in the current directory.
./program
- change directory
cd /path/to/program
./program
- the location where the system looks for executable files
export PATH=$PATH:/path/to/program
program
- Create a startup script file, write the command to start the program in the script file, give it executable permissions, and then start the program by executing the script file, for example:
#!/bin/bash
/path/to/program
Save as start.sh and then run it.
chmod +x start.sh
./start.sh
- systemd: the init system used by many Linux distributions
1. 创建一个服务文件,比如`/etc/systemd/system/program.service`,内容如下:
[Unit]
Description=My Program
After=network.target
[Service]
ExecStart=/path/to/program
[Install]
WantedBy=default.target
- After saving the file, run the following command to enable the service:
sudo systemctl enable program.service
- Then you can use the following commands to start, stop, or restart the service:
sudo systemctl start program.service
sudo systemctl stop program.service
sudo systemctl restart program.service
No matter how you start the program, you can use “&” at the end of the command line to run the program in the background, for example:
./program &