There are several common ways to run programs in the background of Linux.
- ctrl+z
- screen
- nohup
- supervisor
- systemd
The following practice is based on the Ubuntu2204
system, but theoretically other Linux distributions are also applicable.
1. Distinguish usage scenarios
If the command takes a little longer to execute and you don’t need to see the output, use &
as a background run flag or use ctrl+z
to hang the process in the background after running, combined with jobs
and fg
to switch the background tasks of the current session.
If you want the command to stay running after the session ends, consider using screen
and nohup
, screen
for short time tasks (e.g. scripts) and nohup
for long time tasks (e.g. proxies).
If there is a need for logging/permission control, screen
and nohup
are not enough, and it is appropriate to use supervisor
and systemd
and other daemon programs to create background daemons (web applications).
2. Usage
2.1. ctrl-z
Take ping
as an example, send 4 icmp packets to 114.114.114.114 and record the result.
|
|
The effect of the &
symbol is similar to ctrl-z
after running, so if you don’t want to record the results and want to switch back to view them easily at any time, consider using ctrl+z
to suspend the process to the background.
|
|
After hanging, use jobs -l
to view the hung background processes.
Use the command fg %num
to bring the background task process back up and you can see that the process is executing again.
The above methods are only valid for the current session, if the session ends, the program will end its run.
2.2. screen
If you want the program to continue running after the session ends, consider using screen
, which some Linux distributions need to install themselves.
|
|
Use screen to create a ping session.
|
|
After exiting the session, recreate the session and use the create ping session command again.
|
|
You can see that the command is still running.
2.3. nohup
screen
is more suitable for executing some scripts than nohup
, while nohup is suitable for running long-time tasks. nohup is called no hang up
, which means running the program without hanging up, ignoring the system’s Hang Up
signal.
Searching the web for ways to use nohup
, the more common ways to run it are as follows.
|
|
What is more puzzling here is the meaning of 2>&1
.
There are three types of inputs and outputs defined in Linux, which are
- 0: standard input
- 1: standard output
- 2: Error output
The standard output 1
can be left unwritten at runtime, so executing command > command.log
is equivalent to command 1 > command.log
, and after understanding this, the meaning of 2>&1
becomes clearer, indicating that the error output 2
is redirected to the standard output variable &1
, and then combined with >
to be output to the log in the log.
Writing the full command is equivalent to the following.
|
|
Note: nohup command 2>&1 > command.log & is incorrectly written, the error output (2) is redirected to the standard output (1) and subsequently the standard output (1) is redirected to the file, so the error output is disabled.
As an example, take the nc
program to scan the port and execute it with nuhup as follows.
|
|
Check the /tmp/nc.log
file and you can see that it is executing.
nohup
is still very good for performing simple background tasks, but if you have requirements for output log cutting, running users, booting, etc., you need to consider daemon type programs such as supervisor
and systemd
.
2.4. supervisor
supervisor
is a lightweight process control system similar to systemd
, used to run programs for long periods of time, often used for web background programs, proxy programs, etc. On most Linux distributions you need to install it yourself, e.g. install and start the service in ubuntu2204
.
supervisor
supports very rich parameter settings, environment settings and daemons compared to nohup
. After installing supervisor, take the synchronization software syncthing
as an example.
Create the /etc/supervisor/conf.d/syncthing.conf
file, as follows.
|
|
Most of the parameters are very simple and easy to understand, where
- autorestart: boot self-start
- stdout_*: standard output log
- strerr_*: exception output log
- user: the user who runs the program
- environment: set the environment parameters for running
Makes the newly created service effective.
|
|
supervisorctl supports multiple operations, such as
- update: means update the list of services and re-run the new/modified services
- status: check the running status of all services
- start/stop/restart: start/stop/restart the service
2.5. systemd
After 2015, systemd
is the system suite that comes with most distributions to replace SysV init
, providing reliable parallelism during the boot process and centralized management of processes, daemons, services and mount points.
If you are familiar with modern Linux operating systems, you will most likely not be unfamiliar with systemd
. systemd
is a relatively heavyweight system suite, and unless there is a clear need to use it, it is generally not considered as a way to run small applications in the background. supervisor is sufficient for most application scenarios.
The systemd
common user unit configuration directory is as follows (as distinct from the system unit configuration)
- $HOME/.config/systemd/user
- /usr/lib/systemd/user
- $HOME/.local/share/systemd/user
- /etc/systemd/user
Take the synchronization software syncthing
as an example, add a configuration file $HOME/.config/systemd/user/syncthing.service
with the following content.
|
|
Start the service.
|
|
If you modify the configuration sheet, you need to run a refresh of systemd.
|
|
The following is a description of the key parts of the configuration file.
|
|