Linux terminal shutdown background process also ends cause analysis and use of nohup

Source: Internet
Author: User
Tags session id

The Windows and Linux remote connections (called remote connections) are different:

1) After Windows remote connection, if you run a program on a remote machine, you can completely exit the connection after a period of time before connecting to that machine to see if there is an end, that is, Windows remote connection without any processing, the program running on the remote machine will not end with the abort connection.

2) Ubuntu after the terminal login to the remote host and run the program, if this time directly abort the connection to exit, then the session opened by the connection to run all processes will be killed, that is, the remote running program will be terminated with the termination of the connection.

Windows is easy to use for this feature, and Ubuntu is much easier to work with if it can be used to meet the features of Windows responsiveness.

This is true in Ubuntu, mainly because:

1) Related concepts of the process:

In Linux, each process belongs to a single process group (group), and the process group has a team leader; multiple process groups form a session, and the session is established by the process, which is called the session's lead process (leader). The PID of the session leader process becomes the SID (Session ID) of the recognition session. Each process group in a session is referred to as a work (job). A session can have one process group as the foreground work for the session (foreground job), while the other process groups are background work (background job). Not every process in a process group is a job, and the job is made up of the "son" of the session process, but when the process in the job produces a child process, the child process is not the content of the job.

Each session can be connected to a control terminal. The foreground process group that is passed to the session when the control terminal has input and output. The current process group or the last process in the job end, the background session control process automatically switch to the front end, the signal generated by the terminal, such as CTRL + Z, ctrl+\, will be passed to the foreground process group.

Sessions are primarily built for one terminal, and when we open multiple terminal windows, we actually create multiple terminal sessions. Each session will have its own foreground work and background work. In this way, we increase the level of management and operation for the process.

2) Related concepts of Terminal:

In a UNIX system, a user logs into the system through a terminal and gets a shell process, which becomes the control terminal (controlling Terminal) of the shell process, and the control terminal is the information stored in the PCB, and we know that fork copies the information in the PCB, Therefore, the terminal is also the control terminal of other processes initiated by the shell process. By default (no redirection), the standard input, standard output, and standard error output for each process point to the control terminal, and the process reads the keyboard input from the standard input, that is, the process goes to the standard output or the standard error output to write the output to the monitor. In the control terminal to enter some special control keys can signal to the foreground process, for example, Ctrl-c indicates that sigint,ctrl-represents Sigquit.

Each process can access its control terminal through a special device file,/dev/tty. In fact, each terminal equipment corresponds to a different device file,/dev/tty provides a common interface, a process to access its control terminal can be accessed through the/dev/tty or the corresponding device files. The Ttyname function can be used to identify the corresponding file name by a file descriptor, which must point to an end device and not to any file. The command TTY on Linux can also be viewed to the current terminal. For example, we open a terminal in the graphical interface may be/dev/pts/0, the second may be/dev/pts/1. (Network terminal), and switching to the character interface may be/dev/tty1 ... (Virtual terminal)

3) process and terminal:

When a user logs into the system through a terminal, a shell process is obtained, and the shell is controlled not by the process but by the job or process group. A foreground job can consist of multiple processes, a background job can also consist of multiple processes, and the shell can run both a foreground job and any number of background jobs, called Job control (Jobcontrol). For example, start 5 processes with the following command (this example originates from Apue):
$ Proc1 | PROC2 &

$ PROC3 | proc4 | Proc5

where Proc1 and proc2 belong to the same background process group, PROC3, proc4, and proc5 belong to the same foreground process group, and the shell process itself belongs to a separate process group. The control terminals of these process groups are the same, they belong to the same session, and a session is related to a control terminal. When a user enters a special control key (for example, Ctrl-c) in the control terminal, the kernel sends a corresponding signal (for example, SIGINT) to all processes in the foreground process group. The relationships between the processes, the process group, and the session are as shown.


In the example above, PROC3, proc4, and PROC5 are placed by the shell in the same foreground process group, where one process is the Leader,shell call of the process group wait for them to run to the end. Once they are all running, the shell calls the Tcsetpgrp function to refer to the foreground to continue accepting commands. Note, however, that if a process in proc3, proc4, and Proc5 also fork out the child process, the child process belongs to the same process group, but the shell does not know that the child process exists and does not call wait to end it. In other words, PROC3 | proc4 | PROC5 is the shell's job, and this subprocess is not, which is a conceptual difference between a job and a group of processes. Once the job runs, the shell mentions itself to the foreground, and if the original foreground process group still exists (if the child process has not yet terminated), it automatically becomes the background process and is taken over by the INIT process.

Linux of the Normal Process ( except daemon ) is a The child process of the terminal, the existence of the process depends on the terminal to provide it with space including standard input, standard output, standard error. when the initial process of opening a terminal (that is, the SID process) exits, its child processes also end.

4 the process of terminal login and execution of commands:

We now look at the process of logging in and executing commands from the perspective of the session and the process group.

The Getty or telnetd process calls the SETSID function to create a new session before opening the end device, which is called the Sessionleader, and the ID of the process can be seen as the ID of the session. The process then opens the terminal device as the control terminal for all processes in the session. Creating a new session also creates a new process group, the process group Leader, which is the ID of the process group.

During the login process, the Getty or telnetd process becomes login and then becomes the shell, but still the same process, still the session Leader.

Sub-processes that are forked out by the shell process (for example, P3,P4,P5 in the previous example) have the same session, process group, and control terminal as the shell, but the shell calls the Setpgid function to designate a child process in the job as a new process group. Leader (for example, p3), and then calls Setpgid to transfer the other child processes in the job to the process group. If the process group needs to run in the foreground, call the TCSETPGRP function to set it as the foreground process group, and the process group that the shell is in will automatically become a background process group because there can be only one foreground process group in a session.

5) The method to continue execution of the terminal exit process

Unix/linux generally like to let a program run in the background, many use & at the end of the program to let the program run automatically. Maybe our program is just a normal program (not a daemon), generally this program uses & end, but if the terminal is closed, then the program will be closed.

Using the Nohup command, you can execute the program in the background and the terminal exits still running:

Nohup Make &

This screen tip:

[~]$ appending output to Nohup.out

Prove that the operation is successful, and put the output information of the program running into the Nohup.out file of the current directory.

Remember to exit the terminal should use the normal exit method, rather than directly shut down putty or other terminal login program, should be in the session exit. This point be deciphered.

Attached: nohup Command Reference

Nohup command

Purpose: To run the command without hanging off.

Syntax: Nohup Command [Arg ...] [&]

Description: The nohup command runs commands specified by the command parameter and any related ARG parameters, ignoring all hang-up (SIGHUP) signals. Use the Nohup command to run a program in the background after logging off. To run the Nohup command in the background, add & (the symbol representing "and") to the end of the command.

The output is appended to the Nohup.out file in the current directory, regardless of whether the output of the Nohup command is redirected to the terminal. If the nohup.out file for the current directory is not writable, the output is redirected to the $HOME/nohup.out file. If no file can be created or opened for appending, then the command specified by the commands parameter is not callable. If the standard error is a terminal, then all output of the specified command to the standard error is redirected to the same file descriptor as the standard output.

Other methods refer to making Linux processes unaffected by the terminal

Reference:

Http://www.educity.cn/linux/1241877.html

Http://www.linuxidc.com/Linux/2010-09/28366.htm

Http://blog.sina.com.cn/s/blog_70e94af80100lmep.html

Linux terminal shutdown background process also ends cause analysis and use of 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.