How to run and control background processes in Linux: nohup, setsid, &, disown, screen

Source: Internet
Author: User
How to run and control background processes in Linux: nohup, setsid, &, disown, screen

From http://heylinux.com/archives/1282.html#more-1282

 

We often encounter this problem. We used SSH to log on to a remote Linux server and run some time-consuming tasks. As a result, the task failed midway through due to unstable network conditions.
This is because when a user logs out or the network is disconnected, the supervisor will receive an HUP signal to close all its sub-processes.
There are two solutions: 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.

The following describes how to run and control background processes in Linux:
1. nohup
as the name suggests, the purpose of nohup is to let the submitted command ignore all hangup signals.
usage: nohup Command [Arg]...

2. setsid
run commands in a new session to avoid the HUP signal sent by the current terminal.
usage: setsid Command [Arg]...

3 .&
You can combine () to generate a new sub-shell and place the task in the background for running in this sub-shell, so as not to be affected by the HUP signal of the Current Shell terminal.
Usage: (command [Arg]... &)

In general, I use the following methods:
Nohup./filename. Sh> filename. log 2> & 1 &
Three reasons:
1) nohup ensures that the process will not be interrupted due to abnormal hangup signals;
2) Place the task in the background to run without occupying the current terminal;
3) print the error output to the log. The default value is> only the standard output, but not the error output.


4. Process Control
Use the following command to control the commands placed in the background

View the background processes of the current terminal:
Direct execution: jobs

Put a backend process to the foreground:
Enter: FG {jobid} // here {jobid} is the number in [] before the process seen in the jobs command.

Run the processes currently running on the foreground in the background:
Press the shortcut key Ctrl + z // to pause the currently running process.
Run: BG

Terminate the processes currently running on the foreground:
Press the shortcut key Ctrl + C.

5. disown
To ignore the HUP signal for processes that do not use nohup and setsid.
Usage:
Run the processes currently running on the foreground in the background (CTRL + Z and BG );
Then run disown-H % {jobid} // here {jobid} is the number in [] before the process seen in the jobs command.

6. Use screen to achieve stable background operations
Screen is a new full-screen virtual session terminal. This session will exit only when you manually enter exit, the commands executed in this session do not have to worry that the HUP signal will affect our processes. Therefore, you do not need to add "nohup" or "setsid" before each command, it is very suitable for us to have planned to execute a large number of background tasks, which can be very convenient for us to manage these background tasks.

Usage:
Screen // create and enter a session immediately.
Screen-DMs {name} // create a session in disconnected mode and specify the session name as needed.
Screen-list // list all sessions.
Screen-r {name} // enter the specified session.
CTRL + AD // enter the shortcut keys Ctrl + A and D to temporarily exit the current session.
Exit // enter the specified session and execute exit to close the session.

References: https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/

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.