How to start a program in Linux

In Linux, there are several ways to start a program.

  1. Typing the executable file path directly in the command line, for example:
/path/to/program

Alternatively, use the program in the current directory.

./program
  1. change directory
cd /path/to/program
./program
  1. the location where the system looks for executable files
export PATH=$PATH:/path/to/program
program
  1. 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
  1. 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
  1. After saving the file, run the following command to enable the service:
sudo systemctl enable program.service
  1. 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 &
bannerAds