Linux tips: several methods to make processes run reliably in the background

Source: Internet
Author: User
Article Title: Linux tips: several methods to make processes run reliably in the background. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Do you want to keep the process running after it is disconnected? If the process has already started running, how can this problem be rectified? If there are a large number of such requirements, how can we simplify the operation?
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?

Reason for hangup name
In earlier versions of Unix, each terminal communicates 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 an 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

[Root @ pvcent107 ~] # Nohup ping www.ibm.com &
[1] 3059
Nohup: appending output to 'nohup. out'
[Root @ pvcent107 ~] # Ps-ef | grep 3059
Root 3059 984 0 00:00:00 pts/3 ping www.ibm.com
Root 3067 984 0 00:00:00 pts/3 grep 3059
[Root @ pvcent107 ~] #

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

[Root @ pvcent107 ~] # Setsid ping www.ibm.com
[Root @ pvcent107 ~] # Ps-ef | grep www.ibm.com
Root 31094 1 0 07:28? 00:00:00 ping www.ibm.com
Root 31102 29217 0 00:00:00 pts/4 grep www.ibm.com
[Root @ pvcent107 ~] #


 

[1] [2] [3] [4] Next page

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.