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

Source: Internet
Author: User
Tags session id sessions

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

1) After Windows Remote connection, assume that a program is executed on a remote machine, it is completely possible to exit the connection after a period of time before connecting to that machine to see if it is over. That is, the remote connection to Windows is not handled in any case. Programs executed on the remote machine do not end with aborting the connection.

2) Ubuntu after the terminal logs on to the remote host and executes the program. Assuming that the connection exits at this point, all processes executed under the session that the connection is opened to will be killed, that is, the program that executes remotely will end with the abort connection.

Windows is easy to use for this feature, and Ubuntu assumes that it will be generous to work with the features of Windows responsiveness.

The reason for this is in Ubuntu, mainly because:

1) Related concepts of the process:

In Linux. Each process belongs to a process group, and the process group has a team leader. Multiple process groups form a single session, which is established by the process in which the session is called the lead process of the conversation (sessions 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 the content in the job. A job is made up of a "son" directly from the session process. However, when the process in the job produces child processes, 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. At the end of the current process group, or the last process in the job, the session control process in the background actively switches to the front end, generating signals from the terminal. For example, CTRL + Z, ctrl+\, will be passed to the foreground process group.

The session is primarily built for a terminal when we open multiple terminal forms. In fact, multiple terminal sessions are created. Each session will have its own foreground work and background work. Such We have added a hierarchy of management and execution to the process.

2) Related concepts of Terminal:

In UNIX systems, users log into the system via a terminal and get a shell process, which becomes the control terminal (controlling Terminal) of the shell process. The control terminal is the information stored in the PCB, and we know that fork will copy 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), each process's standard input, standard output, and standard error output point to the control terminal, which reads the keyboard input from the standard input, which is read by the user, and 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, such as ctrl-c means that sigint,ctrl-represents sigquit.

Each process is able to access its control terminal via a special device file,/dev/tty.

In fact, each terminal equipment is corresponding to a different device file,/dev/tty provides a common interface. A process to access its control terminal can be through the/dev/tty can also be through the device of the corresponding equipment files to access. The Ttyname function can be traced by the file description descriptor to the corresponding file name, the file descriptive descriptor must point to an end device and not a random file. The command TTY on Linux is also able to view 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 be composed of multiple processes, a background job can be composed of multiple processes, the shell can execute a foreground job at the same time and arbitrary multiple background jobs, which is called Job control (Jobcontrol).

For example, use the following command to start 5 processes (this sample is from Apue):
$ Proc1 | PROC2 &

$ PROC3 | proc4 | Proc5

Between 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 similarly, they belong to the same session. A session is associated with a control terminal. When the user enters a special control key (such as CTRL-C) in the control terminal, the kernel sends a corresponding signal (such as SIGINT) to all processes in the foreground process group. The relationships between the processes, the group of processes, and the session, for example, are seen.


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 to wait for the process group to finish. Once all of them have finished executing, the shell calls the Tcsetpgrp function to refer to the foreground to continue accepting commands. But be careful. Assuming that 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 child process is not, which is a conceptual difference between a job and a process group.

Once the job execution is finished, the shell mentions itself to the foreground. Assuming that the original foreground process group still exists (assuming that the child process has not yet been terminated), it becomes the background process itself 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 Run command:

Now we look at the process of logging in and running commands again 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.

At the same time that a new session was created, a new process group was created, which is the process group Leader of the processes and the ID of the process group.

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

A child process that is forked out by the shell process (for example, P3,P4 in the previous example). P5) had the same session, process group, and control terminal as the shell. However, the shell calls the Setpgid function to designate a child process in the job as Leader of a new process group (say P3). Then call Setpgid to transfer the other child processes in the job to the process group as well. Assuming that the process group needs to be executed in the foreground, call the TCSETPGRP function to set it as the foreground process group. Because a session can have only one foreground process group, the process group in which the Shell resides is itself actively turned into a background process group.

5) How the terminal exit process continues to run

Unix/linux generally want to let a program in the background, very much is to use & at the end of the program to let the program self-executing. Perhaps our program is just a normal program (not a daemon), the general process of using & end. But suppose the terminal is off. Then the program will be closed.

Use the Nohup command to execute the program in the background and the terminal exit still executes:

Nohup Make &

This screen tip:

[~]$ appending output to Nohup.out

Prove that execution succeeds, and at the same time put the output information of the program execution into the Nohup.out file of the current folder.

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

Attached: nohup command references

Nohup command

Purpose: Executes the command without hanging up.

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

Descriptive narrative: The Nohup command executes the command specified by the order parameter and regardless of the ARG parameter in relation to it. Ignore all hang-up (SIGHUP) signals.

Use the Nohup command to execute a program in the background after logging off. The Nohup command to execute in the background. Add & (Symbol for "and") to the tail of the command.

Regardless of whether the output of the Nohup command is redirected to the terminal, the output is appended to the Nohup.out file in the current folder. Assume that the Nohup.out file for the current folder is not writable. The output is redirected to the $HOME/nohup.out file. Assuming that no file can be created or opened for appending, the command parameter specified is not callable. Suppose the standard error is a terminal. Then redirect the specified command to the full output of the standard error as standard output to the same file description descriptor.

Other ways to make Linux processes unaffected by the terminal

References:

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

Related Article

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.