Several methods for running Linux processes in the background

Source: Internet
Author: User
We often encounter this problem. We use telnet/ssh to log on to a remote Linux server and run some time-consuming tasks. as a result, the task fails midway through due to network instability. After the command is submitted, how does one prevent the local terminal window/network disconnection? Let's take the following example... we often encounter this problem. We use telnet/ssh to log on to a remote Linux server and run some time-consuming tasks. as a result, the task fails midway through due to network instability. After the command is submitted, how does one prevent the local terminal window/network disconnection? Here are some examples. you can select different methods for different scenarios to solve this problem.
Nohup/setsid /&
Scenario: if a temporary command needs to be run for a long time, what method can be used to ensure its stable operation in the background? In earlier versions of Unix, hangup names communicate with the system through modem. When a user logs out, the modem hangs up. Similarly, when the modem is disconnected, it will send an hangup signal to the terminal to notify it to close all sub-processes.
Solution: We know that when a user logs out or the network is disconnected, the supervisor will receive a HUP signal to close all its sub-processes. Therefore, our solution has two ways: either let the process ignore the HUP signal, or let the process run in a new session to become a child process that does not belong to this terminal.
1. nohup
Nohup is undoubtedly the first method we have come up. As the name suggests, nohup is used to make the submitted command ignore the hangup signal. Let's take a look at the Help information of nohup: NOHUP (1) User Commands NOHUP (1)
NAME nohup-run a command immune to hangups, with output to a non-tty
SYNOPSIS nohup COMMAND [ARG]……
Nohup OPTION
DESCRIPTION Run COMMAND, ignoring hangup signals.
-- Help display this help and exit
-- Version output version information and exit
It can be seen that nohup is very convenient to use. you only need to add nohup before the command to be processed. The standard output and standard error will be redirected to the nohup. out file by default. Generally, you can add "&" at the end to run the command in the background, or use "> filename 2> & 1" to change the default redirection file name.
Nohup example
# Nohup ping www.2cto.com &
[1] 3059 nohup: appending output to 'nohup. out'
# Ps-ef | grep 3059
Root 3059 984 0 00:00:00 pts/3 00:00:00 ping www.2cto.com root 3067 984 0 pts/3
Grep 3059
2. setsid
Nohup can undoubtedly avoid interruption of our process by ignoring the HUP signal. However, if we think from another perspective that our process is not a sub-process of the terminal that accepts the HUP signal, then it will naturally not be affected by the HUP signal. Setsid can help us do this. Let's take a look at the Help information of setsid: SETSID (8) Linux Programmer's Manual SETSID (8)
NAME setsid-run a program in a new session
SYNOPSIS setsid program [arg ...... ]
DESCRIPTION setsid runs a program in a new session.
It can be seen that the use of setsid is also very convenient, you only need to add setsid before the command to be processed.
Setsid example
# Setsid ping www.2cto.com
# Ps-ef | grep www.2cto.com
Root 31094 1 0 07:28? 00:00:00 ping www.2cto.com root 31102 29217 0 00:00:00 pts/4
Grep www.2cto.com
In the preceding example, the process ID (PID) is 31094, and its parent ID (PPID) is 1 (that is, the init process ID ), it is not the process ID of the current terminal. compare this example with the parent ID in the nohup example.
3 .&
Here is also a tip about subshell. We know that the inclusion of one or more names in "()" allows these commands to run in the sub-shell, thus extending many interesting functions, we will discuss one of them now.
When we put "&" into "()", we will find that the submitted jobs are not in the job list, that is, they cannot be viewed through jobs. Let's take a look at why we can escape the influence of the HUP signal.
Subshell example
# (Ping www.2cto.com &)
# Ps-ef | grep www.2cto.com root 16270 1 0 00:00:00 pts/4 00:00:00 ping www.2cto.com root 16278 15362 0 pts/4
Grep www.2cto.com
From the above example, we can see that the parent ID (PPID) of the newly submitted process is 1 (PID of the init process) and is not the process ID of the current terminal. Therefore, it does not belong to the sub-process of the current terminal, so it will not be affected by the HUP signal of the current terminal.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.