We will be sharing several ways to run background tasks under linux: &
, nohup
, ctrl + z
, jobs
, fg
, bg
, and screen
commands.
1. Introduction of the problem
The most intolerable thing for programmers is that when using the terminal, often because of the network, close the screen, perform CTRL + C and other reasons caused ssh disconnected caused by running programs to exit, making our work lost.
The main reason behind the above-mentioned operations, the shell by default will send an interrupt signal to the terminal session associated with the process, thus causing the process to follow the terminal exit, in order to clarify this issue we first need to understand the two interrupt signals:
-
sigint
: signal interrupt, ctrl+c will send this signal to actively close the program -
sighup
: signal hang up, close the terminal, network disconnection, close the screen will send this hang up signal.
Today, we will introduce several methods of executing background tasks in linux to avoid the above problems.
2. &
symbol
This is a kind of & at the end of the execution command, so that the program started to ignore the sigint signal, at this time the execution of ctrl + c close will not close the process, but when the screen is closed, disconnected from the network will still cause the process to exit.
sh test.sh &
3. nohup
command
nohup (no hang up), meaning no hang up, run command with nohup can make the command execute permanently, and there is no relationship with the user terminal, disconnect SSH does not affect the operation, nohup captures the SIGHUP, and do ignore the processing, so when the screen is closed, disconnected, etc. cause ssh interruption process will not exit. But ctrl+c can close the process to shut it down. So in most cases programs that are started with both nohup and &, ctrl+c and closing the terminal will not close them. By default all output is redirected to a file called nohup.out.
The basic usage format of the nohup command is
nohup Command [ Arg ... ] [ & ]
Example
Execute without interruption in the background. /test.sh, stdout is output to out.log
, stderr
is output to err.log
nohup . /test.sh > out.log 2>err.log &
The meaning of the associated numbers is as follows.
0 - stdin
(standard input).
1 - stdout
(standard output), obviously nohup command > out.log is equivalent to nohup command 1> out.log, which is the default behavior.
2 - stderr
(standard error)
You may also see this written in such a way that it means that stderr is also redirected to stdin
nohup . /test.sh > out.log 2>&1 &
4. ctrl + z, jobs
, fg
, bg
What if our program doesn’t use &
, nohup
at startup? Do we need to execute ctrl + c
to terminate the process executing in the foreground before restarting it? There’s obviously a good way to do it!
4.1 ctrl + z
Put a job process that is executing in the foreground into the background and suspend it, or in terminology, hang it, and then execute it as follows.
[1]+ Stopped . /test.sh
4.2 jobs
Check how many commands are currently running in the background, [jobnumber] is the job number.
jobs
[1]+ Stopped . /test.sh
[2]+ Running . /test2.sh &
4.3 bg
Continue running a suspended (hung) job in the background, e.g. put job #1 (. /test.sh) into the background, and note that it is already running with &
bg 1
[1]+ . /test.sh &
4.4 fg
Move the job process in the background to the foreground to continue running, e.g. move job #2 (. /test2.sh &) to the foreground
fg 2
. /test2.sh
5. screen commands
5.1 Introduction
If the above method implements front and backend task scheduling through linux-related commands themselves, screen offers another way of thinking.
The non-human version: GNU Screen is a free software developed by the GNU Project for command-line terminal switching. GNU Screen can be thought of as a command-line interface version of the Window Manager. It provides a unified interface for managing multiple sessions and the corresponding functions.
The human version: We can roughly think of screen as a virtual terminal software that starts another background program directly inside the linux system that takes over (maintains) your terminal session, so that when your directly connected terminal ssh is disconnected, it still makes the program think that your ssh is continuously connected, so that the process does not receive an interruption signal and exit.
5.2 Installation
yum install screen
5.3 Using
- Create a new session
screen -S yourname -> create a new session called yourname
- List all current sessions
screen -ls
- Resume the session (back to the session called yourname)
screen -r yourname
- detach a session
screen -d yourname -> detach a session remotely screen -d -r yourname -> end the current session and return to the session yourname
- Delete the session
screen -S pid-X quit