Linux back-end run

Source: Internet
Author: User

Program Command &: Put commands in the background to run.

Ctrl + Z: Move a running front-end command to run in the background, it is equivalent to: Program Command &, so although the program is placed in the back-end run, but at this time the program state is paused, as follows:

[1]+ Stopped Zendstudio/zendstudio; At this point, you can enter a command BG to turn the back-end program into a running state in the paused state.

BG: Change the back-end program from paused to running state, such as: [1]+ Zendstudio/zendstudio &

FG: The program in the back end (in the running and paused state) is tuned to the front end for display.

--------------------------------------------------------------------------------

Linux process backend running, off-terminal

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.

nohup/setsid/&

Scene:

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?

The origins of the Hangup name are 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.

Workaround:

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.

1. Nohup

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 COMM And [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

[Email protected] ~]# nohup ping www.ibm.com &
[1] 3059 nohup:appending output to ' nohup.out '
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

[Email protected] ~]# Setsid Ping www.ibm.com
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. &

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

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 Protec Ted] ~]#

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.

Linux back-end run

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.