The currently running process remains after SSH is closed
For a variety of reasons, you need to login to the Linux or Unix host via SSH, many times we need to start some services via SSH terminal or run some programs, but by default, when we close the SSH Terminal connection, the program that is started will also be closed. The reason is that when the SSH session is closed, the pty associated with SSH is closed and the system sends SIGHUP signals to all processes in the session associated with the Pty, and SIGHUP's default signal handler is to terminate the process, unless the process handles SIGHUP itself.
Here's how to fix it:
With out-of-the-box command nohup, you can have the specified program continue to run after Pty is closed.
How to run:
#nohup Program &
Now you can shut down SSH normally, you will find that your service is still running
When the SECURECRT closes unexpectedly, the background process shuts down together with the culprit signup signal
Author: 2hei posted on May 19, 2009 22:24 Copyright notice: You can reprint, reprint must be in the form of hyperlinks to the original source of the article and the author information and copyright notice http://www.2hei.net/mt/2009/05/ Securecrt-closed-and-sighup.html
Symptom: Use the SECURECRT tool SSH remote connection Linux, do not quit SSH, but forcibly shut down the terminal (close the notebook to leave), the background of the application will be turned off.
Consequence: A man cries, cry, not sin!
Test Case:
It is obvious that after the terminal is closed, the foreground running program will be closed, but the background process will also be closed due to terminal abnormal shutdown, as follows is a simple test results:
1, the background executes the program, closes the SSH connection normally, shuts down the terminal condition:
Turn on two terminals, where the first ping is done in the background
Ping google.com | Tee Log.txt &
Observed at the second terminal
Tail-f Log.txt can see that the log has been written
Use the Pstree command to see that the ping process exists
|-SSHD-+-SSHD---sshd---bash---pstree
| '-sshd---sshd---bash-+-ping
| '-tee
Ctrl+d exit the first terminal and then close the terminal
The log of the second terminal is still in writing, Pstree command:
|-ping
PS X
29427? S 0:00 Ping google.com
The process of seeing the ping still exists
2, the background execution program, abnormal shut down the SSH connection (analog sudden power down or, not have time to quit SSH, or SSH still connected, directly shut down terminal SECURECRT) Situation:
Also open two terminals, the first of which pings in the background
Ping google.com | Tee Log.txt &
Observed at the second terminal
Tail-f Log.txt can see that the log has been written
Use the Pstree command to see that the ping process exists
|-SSHD-+-SSHD---sshd---bash---pstree
| '-sshd---sshd---bash-+-ping
| '-tee
Forcibly shut down the first terminal (SECURECRT) without exiting SSH
Observed at the second terminal
Tail-f Log.txt can see that the log has stopped writing.
Use the Pstree command to see that the ping process is stopped
PS aux does not see the ping process, indicating the forced shutdown of the terminal in the case of the background process also do not kill off
Check the information, and the key is the sighup signal.
To view all signals:
Kill-l
1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill
5) SIGTRAP 6) SIGABRT 7) Sigbus 8) SIGFPE
9) SIGKILL) SIGUSR1 SIGSEGV) SIGUSR2
Sigpipe) sigalrm SIGTERM) SIGCHLD
Sigcont) SIGSTOP SIGTSTP) Sigttin
Sigttou) Sigurg sigxcpu) Sigxfsz
() sigvtalrm) sigprof sigwinch) SIGIO
SIGPWR) Sigsys sigrtmin) sigrtmin+1
sigrtmin+2) sigrtmin+3 (sigrtmin+4) sigrtmin+5
sigrtmin+6) sigrtmin+7 sigrtmin+8) sigrtmin+9
(sigrtmin+10) sigrtmin+11 () sigrtmin+12) sigrtmin+13
SIGRTMIN+14) sigrtmin+15 SIGRTMAX-14) SIGRTMAX-13
SIGRTMAX-12) SIGRTMAX-11 SIGRTMAX-10) SIGRTMAX-9
SIGRTMAX-8) (SIGRTMAX-7) SIGRTMAX-6) SIGRTMAX-5
SIGRTMAX-4) (SIGRTMAX-3) SIGRTMAX-2) SIGRTMAX-1
) Sigrtmax
In the list, the signals numbered 1 ~ 31 are signals that are supported by traditional UNIX, are unreliable signals (not real-time), and the signals numbered 32 to 63 are later expanded, called reliable signals (real-time signals). The difference between unreliable and reliable signals is that the former does not support queueing and may cause signal loss, while the latter does not.
The sighup signal is emitted at the end of the user terminal connection (normal or abnormal), usually at the end of the control process of the terminal, notifying the individual jobs within the same session, when they are no longer associated with the control terminal.
When you log in to Linux, the system is assigned to a terminal (Session) of the logged-on user. All programs running at this terminal, including the foreground process group and the background process group, usually belong to this Session.
When the user exits the Linux login, the foreground process group and the background will receive a sighup signal to the terminal output process. The default action for this signal is to terminate the process, so the foreground process group and the process that has terminal output in the background will be aborted.
However, some programs can capture this signal, and ignore it, so that even if you quit the Linux login, the daemon is still running.
In addition, for daemons with terminal disengagement, this signal is used to notify it to reread the configuration file.
The following is a network of relevant information
Http://hi.baidu.com/xingfengsoft/blog/item/fedbd05c4d8c7e45faf2c0c8.html
--SECURECRT What is the difference between an exception exit and exit execution?
If you close securecrt (this is assumed to be using SSH login terminal), then for the system being logged on, the remote program is abnormally disconnected. And we suddenly cut off the net is the same effect.
In this case, the user does not send a signal, but the SSHD service detects a peer response timeout, and then sends an end signal to the previously established connection and the process under which the SSH login will be assigned a bash to the user. If a part of the process ignores the signal sent by sshd, the process does not exit and the process will be taken over by the INIT process after bash assigned to the user exits.
The workaround for the background process not to close after the terminal exception exits:
1. Use nohup command: nohup <command> [argument ...] & Nohup can block SIGHUP signal!
2. Use the screen command.
The following is a brief introduction:
Screen vi/tmp/2hei.net
The connection needs to be interrupted:
Screen Ctrl + A D
To query the screen process:
PS X
20377? Ss 0:00 Screen Vi/tmp/2hei.net
Re-connect Screen-r 20377
Resources:
Http://www.ibm.com/developerworks/cn/linux/l-cn-screen
Linux jobs and other pre-and post-run command solutions
A,shell supports function control, with the following commands:
1. command& let the process run in the background
2. Jobs view the process running in the background
3. FG%n Let the background run process N to the foreground
4. BG%n let process n Go backstage;
PS: "N" is the process number that jobs looked up.
B. The following transfers: http://blog.chinaunix.net/u/1604/showart_1079559.html
FG, BG, Jobs, &, CTRL + Z are all related to system tasks, although these commands are largely not needed now, but they are also useful
One. & is most often used
This is used at the end of a command, you can put this command in the background to execute
Two. CTRL + Z
You can put a command that is executing in the foreground in the background and pause
Three. Jobs
See how many commands are currently running in the background
Four. Fg
Move commands in the background to the foreground to continue running
If there are multiple commands in the background, you can use FG%jobnumber to bring up the selected command,%jobnumber is the ordinal (not PID) of the command being executed in the background through the jobs command.
Five. Bg
A command that pauses in the background changes to continue execution
If there are multiple commands in the background, you can use BG%jobnumber to bring up the selected command,%jobnumber is the ordinal (not PID) of the command being executed in the background through the jobs command.
#Linux下使用Shell命令控制任务 Jobs Execution
The following commands can be used to manipulate process tasks:
PS Lists the processes that are running in the system;
Kill sends a signal to one or more processes (often used to kill a process);
Jobs lists the status of tasks that are started in the current shell environment, and if JOBSID is not specified, displays all active task status information, and if a task is reported to be terminated (that is, the status of the task is marked as Terminated), the shell Removes the process identity of the task from the list known to the current shell environment;
BG moves the process to the background (Background);
FG moves the process to the foreground (Foreground);
Move job to Background run
If you often work under X graphics, you might have experienced the following: Running a GUI program with a terminal command, the GUI interface coming out, but your terminal still in place, you can't continue executing other commands in the shell unless you turn the GUI program off.
To enable the terminal to continue accepting commands after the execution of the program, you can move the process to the background and run the program using the following command: #假设要运行xmms
$xmms &
When this opens the XMMS, the terminal prompts are back. Now the XMMS is running in the background, but in case you forget to use "&" When you run the program and don't want to re-execute it, you can use CTRL + Z to suspend the program and then tap the BG command, so the program continues to run in the background.
Concept: Current task
If there are 2 task numbers in the background, [1],[2]; If the first background task executes successfully and the second background task is still in progress, the current task will automatically become a background task for the background task number "[2]". So it can be concluded that the current task is subject to change. When the user enters commands such as FG, BG, and stop, the current task is changed if no quotation marks are added.
See Jobs
Use the jobs or PS command to view the jobs being executed.
The result of the jobs command execution, + represents a current job, the minus table is a job after the current job, the JOBS-L option shows the Pid,jobs status of all tasks can be running, stopped, Terminated, However, if the task is terminated (kill), the shell removes the process identity of the task from the list known to the current shell environment, that is, the jobs command displays the task information that is running in the background or suspended in the current shell environment;
Process hangs
Hang of background process:
Execute through the Stop command in Solaris, view the job number (assuming num) through the jobs command, and then execute the Stop%num;
In Redhat, the Stop command does not exist, and the process can be suspended by executing the command kill-stop PID;
When you want to re-execute the currently suspended task, the status of the suspended job can be changed from stopped to running through BG%num, and the command FG%num can be executed when it needs to be executed in the foreground instead;
Foreground process hangs:
CTRL + Z;
Termination of the process
Termination of the background process:
Method One:
View the job number (assuming num) through the jobs command, and then execute the kill%num
Method Two:
Use the PS command to view the job's process number (PID, assuming PID), and then execute the kill PID
Termination of the foreground process:
CTRL + C
Other effects of Kill
In addition to terminating the process, kill can also send other signals to the process, using kill-l to see the signals that kill supports.
Sigterm is the signal that kill sends without parameters, meaning that the process terminates, but execution depends on whether the process supports it. If the process has not been terminated, you can use the Kill-sigkill PID, which is the kernel to terminate the process and the process cannot listen for this signal.
===================
Unix/linux generally want to let a program run in the background, many use & at the end of the program to let the program run automatically. For example we want to run MySQL in the background:
/usr/local/mysql/bin/mysqld_safe--user=mysql &
However, many of our programs are not as mysqld as the Guardian process, perhaps our program is only ordinary programs, generally this program even use & end, if the terminal is closed, then the program will be closed. In order to be able to run in the background, we need to use the Nohup command, such as we have a start.sh need to run in the background, and want to be able to run in the background, then use Nohup:
Nohup/root/start.sh &
Prompt after carriage return in shell:
[~]$ appending output to Nohup.out
The standard output of the original program is automatically redirected to the Nohup.out file in the current directory, which plays the role of log.
But sometimes in this step there is a problem, when the terminal is closed, the process will be automatically closed, see Nohup.out can be seen in the shutdown Terminal Instant service automatically shut down.
After consulting the Red Flag Linux engineer, he also can not be solved, after executing on my terminal, the process that he starts is still running after shutting down the terminal.
The second time I showed it, I found out that I was in a different detail than when he operated the terminal: he was prompted by the Shell to return to the Shell Input Command window by pressing the keyboard key on the terminal, and then exit the terminal by entering exit in the shell Nohup , and I'm the one that closes the terminal after the Nohup executes successfully after the Close Program button. So this time will be broken the corresponding session of the command, resulting in nohup corresponding process is notified need to shutdown together.
This detail has not been noticed by me, so I recorded it here.
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.
Exit Status: The command returns the following exit values:
126 you can find but not invoke commands specified by the command parameter.
The 127 nohup command has an error or cannot find the command specified by the commands parameter.
Otherwise, the exit status of the Nohup command is the command parameter that specifies the exit state of the commands.
Nohup command and its output file
Nohup command: If you are running a process and you feel that the process will not end when you exit the account, you can use the Nohup command. This command can continue to run the process after you exit the account/close the terminal. Nohup is the meaning of not hanging (n ohang up).
The general form of the command is: Nohup Command &
To submit a job using the Nohup command
If you submit a job using the Nohup command, all output from the job is redirected to a file named Nohup.out by default, unless the output file is specified separately:
Nohup command > Myout.file 2>&1 &
In the example above, the output is redirected to the Myout.file file.
Use jobs to view tasks.
Use FG%n to close.
There are also two commonly used FTP tools Ncftpget and Ncftpput, can be implemented in the background FTP upload and download, so you can use these commands in the background to upload and download files.
Simple and useful nohup command in Unix/linux, the normal process is placed in the background with the & symbol, and if you start the program's console logout, the process terminates.
To implement the daemon, one way is to program according to the rules of the daemon (this site an article introduced), more trouble; another approach is to still program with the normal method and then start the program with the Nohup command:
nohup< Program Name >&
After the console logout, the process continues to function as a daemon (although it is not a strict daemon).
Using the Nohup command, the original program's standard output is automatically redirected to the current directory of the Nohup.out file, played the role of log, the implementation of the full daemon function.
Ygwu @ April 18, 2005 10:03
For example:
How do I remotely start the WebLogic service?
Remotely control the server with Telnet, start the WebLogic service remotely, and then shut down the Telnet,weblogic service when it starts, because a process started with Telnet shuts down as the Telnet process shuts down. So we can use some UNIX commands to do it without shutting down.
Use the following command:
Nohup startweblogic.sh&
If you want to monitor standard output you can use:
Tail-f Nohup.out
When you run a program in the background, you can use the Jobs command to view the status of the background job. To use the FG command with parameters to switch background jobs of different numbers to run on the foreground when there are multiple daemons.
When the user initiates a process, the process is run in the foreground, using the standard input and output associated with the corresponding control terminal for input and output. Even if the input and output of the process is redirected and the process is placed in the background, the process is still related to the current terminal device. Because of this, at the end of the current logon session, the control terminal device will be disconnected from the login process, then the system to all the process associated with the terminal to send Sighup signal, notify the process line has been suspended, if the program does not take over the processing of this signal, then the default reaction is the process end. Therefore, the normal program does not really leave the login session and run the process, in order to make the system log on after the normal execution, only use the command nohup to start the corresponding program.
Using the command nohup can of course start such a program, but the Nohup startup program exits when the process finishes executing, while some common service processes typically run permanently in the background without outputting results to the screen. In Unix, these persistent daemon processes are called daemons (daemon). Daemons usually start automatically from the start of the system and stop when the system shuts down.
In the daemon, the most important one is the Super Daemon inetd, which takes over most of the network services, but does not handle each service on its own, but instead launches different service programs to deal with the client based on the connection request. INETD supports the type of network service defined in its settings file/etc/inet.conf. Each line in the inet.conf file corresponds to a port address, and when inetd accepts a connection request to the port, the appropriate process is initiated for processing. The advantage of using inetd is that the system does not have to start many daemons, thus saving system resources, but using inetd to start the daemon will be slower and not suitable for dense-access service processes
The currently running process remains after SSH is closed