Several ways to keep processes running reliably in the background nohup,setsid,&,disown,ctrl-z, screen

Source: Internet
Author: User

Several ways to keep processes running reliably in the background

A few years ago in the developerworks above to see the article, feel very practical, and simple to organize a bit, go here, hope to see people bring some help. The Nohup and Subshell methods mentioned in the article have been used.

We often encounter such problems, log on to the remote Linux server with TELNET/SSH, run some long-time tasks, the result of the network instability caused by the task Midway failure. How do i make the command submit without being disturbed by the local shutdown of the terminal window/network disconnection? Here are some examples where you can choose different ways to handle this problem for different scenarios.

What is the easiest way to make sure it runs stably in the background if only a temporary command takes a long time to run?

Workaround:

1.nohup

We know that when the user logs off (logout) or the network disconnects, the terminal receives a HUP (hangup) signal to close all its child 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.

Hangup name, in earlier versions of UNIX, each terminal communicates via modem and system. When the user logout, the modem hangs up on the phone. Similarly, when the modem disconnects, it sends a hangup signal to the terminal to notify it to close all child processes.

Nohup is undoubtedly the way we think first. As the name implies, Nohup's purpose is to let the submitted command ignore the hangup signal. Let's take a look at Nohup's help information:

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 the use of nohup is very convenient, just add nohup before the command to be processed, standard output and standard errors are redirected to the Nohup.out file. In general we can add "&" at the end to run the command in the background, or ">filename 2>&1" to change the default redirect file name.

nohup Example

[Root@pvcent107 ~]# nohup ping www.ibm.com &; 

[1] 3059 nohup:appending output to ' nohup.out '

[Email protected] ~]# Ps-ef |grep 3059

Root 3059 984 0 21:06 pts/3 00:00:00 Ping www.ibm.com

Root 3067 984 0 21:06 pts/3 00:00:00 grep 3059

[Email protected] ~]#

2.setsid

Nohup can undoubtedly make our process avoid interruption by ignoring the HUP signal, but if we think in a different way, if our process is not part of the sub-process of the terminal receiving the HUP signal, then naturally it will not be affected by the HUP signal. Setsid can help us do that. Let's take a look at Setsid's help information:

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.

Visible Setsid is also very convenient to use, but also only need to deal with the command before adding Setsid can.

Setsid Example

[Root@pvcent107 ~]# setsid ping www.ibm.com 

[Email protected] ~]# Ps-ef |grep www.ibm.com

Root 31094 1 0 07:28? 00:00:00 Ping www.ibm.com

Root 31102 29217 0 07:29 pts/4 00:00:00 grep www.ibm.com

[Email protected] ~]#

It is worth noting that our process ID (PID) in the example above is 31094, and its parent ID (PPID) is 1 (that is, the Init process ID), not the process ID of the current terminal. Compare this example to the parent ID in the nohup example.

3.& symbols (commands are easy to remember, recommended for use.) )

Here's a little tip about Subshell. We know that the inclusion of one or more names in the "()" allows these commands to run in a child shell, which expands a lot of interesting functionality, and one of the things we're going to talk about right now.

When we put "&" into "()", we will find that the submitted job is not in the job list, that is, it cannot be viewed by jobs. Let's see why it's possible to avoid the effects of HUP signals.

Subshell Example

[Root@pvcent107 ~]# (ping www.ibm.com &;) 

[Email protected] ~]# Ps-ef |grep www.ibm.com

Root 16270 1 0 14:13 pts/4 00:00:00 Ping www.ibm.com

Root 16278 15362 0 14:13 pts/4 00:00:00 grep www.ibm.com

[Email protected] ~]#

As can be seen from the example above, 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.

We already know that the effects of HUP signals can be avoided by adding nohup or setsid prior to the command. But if we have already submitted an order without any processing, how can we remedy it to avoid the effects of the HUP signal?

Workaround:

1.disown

At this time to add nohup or setsid is too late, only through job scheduling and disown to solve the problem. Let's take a look at disown's help information:

Disown [-ar] [-h] [jobspec ...]

Without options, each jobspec was removed from the table of

Active jobs. If the-h option is given, each jobspec are not

Removed from the table, but was marked so this SIGHUP is not

Sent to the job if the shell receives a SIGHUP. If No Jobspec

is present, and neither the-a nor the-r option is supplied,

The current job is used. If no Jobspec is supplied, the-a

option means to remove or mark all jobs; THE-R option without

A jobspec argument restricts operation to running jobs. The

Return value is 0 unless a jobspec does not specify a valid

Job.

As can be seen, we can achieve our goal in the following way.

Flexible use of ctrl-z

In our daily work, we can use the ctrl-z to suspend the current process to a background pause, perform some other action, and then use FG to back up the suspended process to the foreground (or BG to put the pending process in the background) to continue running. This allows us to flexibly switch between running multiple tasks within one terminal, which is especially useful when debugging code. Since the code editor is suspended to the background and then re-placed back, the cursor position remains at the location of the last suspension, avoiding the hassle of repositioning.

Use disown-h Jobspec to make a job ignore hup signals.

Use Disown-ah to make all jobs ignore the HUP signal.

Use DISOWN-RH to make the running job ignore the HUP signal.

It is important to note that when disown is used, the target job will be removed from the job list and we will no longer be able to use jobs to view it, but can still find it with PS-EF.

However, there is a problem, the operation of this method is the job, if we run the command at the end of the "&" to make it a job and run in the background, then it is all right, we can use the jobs command to get a list of all jobs. But if the current command is not running as a job, how can I get its job number? The answer is to use CTRL-Z (hold down the CTRL key while holding down the Z key)!

The purpose of Ctrl-z is to suspend the current process (Suspend), then we can use the Jobs command to query its job number, and then use the BG Jobspec to put it in the background and continue to run. It is important to note that this method is used with caution if the suspension affects the running results of the current process.

Disown Example 1 (You can use "disown" directly if you have already used "&" to run the command in the background when you submit the command)

[Root@pvcent107 build]# cp-r testlargefile largefile &; 

[1] 4825

[[email protected] build]# jobs

[1]+

Running

Cp-i-R testlargefile largefile &;

[[email protected] build]# disown-h%1

[Email protected] build]# Ps-ef |grep largefile

Root 4825 968 1 09:46

PTS/4 00:00:00 cp-i-R testlargefile Largefile

Root 4853 968 0 09:46

PTS/4 00:00:00 grep largefile

[Email protected] build]#

Logout

Disown Example 2 (if the command was submitted without using "&" to run it in the background, you can use Ctrl-z and "BG" to put it in the background and use "disown")

[Email protected] build]# cp-r testlargefile largeFile2

[1]+ Stopped cp-i-R testlargefile LargeFile2

[[email protected] build]# BG%1

[1]+ cp-i-R testlargefile largeFile2 &;

[[email protected] build]# jobs

[1]+ Running cp-i-R testlargefile largeFile2 &;

[[email protected] build]# disown-h%1

[Email protected] build]# Ps-ef |grep largeFile2

Root 5790 5577 1 10:04 pts/3 00:00:00 cp-i-R testlargefile LargeFile2

Root 5824 5577 0 10:05 pts/3 00:00:00 grep largeFile2

[Email protected] build]#

We already know how to keep the process free of HUP signals, but if there are a lot of such commands that need to be run in a stable background, how do you avoid doing this for every command?

Workaround:

1.screen

The most convenient way to do this is screen. Simply put, screen provides the ansi/vt100 terminal emulator, which enables it to run multiple full screens of pseudo-terminals under a real terminal. Screen has a lot of parameters and is very powerful, so we'll just describe its common functions and briefly analyze why using screen can avoid the effects of HUP signals. Let's take a look at screen's help information:

Screen (1) screen (1)

NAME

Screen-screen Manager with Vt100/ansi terminal emulation

Synopsis

screen [-options] [cmd [args]]

Screen-r [[PID.] Tty[.host]]

Screen-r Sessionowner/[[pid.] Tty[.host]]

DESCRIPTION

Screen was a Full-screen window manager that multiplexes a physical

Terminal between several processes (typically interactive shells).

Each virtual terminal provides the functions of a DEC VT100 terminal

And, in addition, several control functions from the ISO 6429 (ECMA

, ANSI X3.64) and ISO 2022 standards (e.g. Insert/delete line and

Support for multiple character sets). There is a scrollback

Buffer for each virtual terminal and a copy-and-paste mechanism that

Allows moving text regions between windows.

Using screen is convenient and has several common options:

Use the SCREEN-DMS session name to create a session in disconnected mode (and specify its session name).

Use Screen-list to list all sessions.

Use Screen-r session name to reconnect to the specified session.

Use the shortcut key Ctrl-a D to temporarily disconnect the current session.

Screen Example

[Root@pvcent107 ~]# SCREEN-DMS Urumchi 

[Email protected] ~]# screen-list

There is a screen On:12842.urumchi (Detached) 1 Socket in/tmp/screens/s-root.

[Email protected] ~]# screen-r Urumchi

When we connect to the screen session with "-R", we can do anything in this pseudo-terminal, no longer worry about the HUP signal will affect our process, and do not have to add "nohup" or "setsid" before each command. What is this for? Let me take a look at the following two examples.

--the process tree without the screen's newly-used process

[[email protected] ~]# ping www.google.com &

[1] 9499

[Email protected] ~]# pstree-h 9499

Init─┬─xvnc

├─acpid

├─atd

├─2*[sendmail]

├─sshd─┬─sshd───bash───pstree

│└─sshd───bash───ping

As we can see, the bash we're in when we're not using screen is an sshd subprocess, and when SSH disconnects, the HUP signal naturally affects all of the sub-processes underneath it (including our newly created ping process).

--The process tree using the new process after screen

[Email protected] ~]# screen-r Urumchi

[[email protected] ~]# Ping www.ibm.com &

[1] 9488

[Email protected] ~]# pstree-h 9488

Init─┬─xvnc

├─acpid

├─atd

├─screen───bash───ping

├─2*[sendmail]

Instead of using screen, bash is the child process of screen, and screen is the child of Init (PID 1). Then when SSH disconnects, the HUP signal naturally does not affect the sub-processes under screen.

Summarize

Now that several methods have been introduced, we can choose different scenarios according to different scenarios. Nohup and Subshell methods are undoubtedly the most convenient way to temporarily need, disown can help us to remedy the work that is currently running, and screen is in the large batch operation is not two choice. The following methods are not common.

Several ways to keep processes running reliably in the background nohup,setsid,&,disown,ctrl-z, screen

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.