@ Wooyun ssh connects to Linux. If you want to disable the connection, you can use the nohup command to run a program in the background.
Nohup/opt/phpfpm/bin/php hotel. php &
Appending output to nohup. out is correct.
You can run ps-ef to view the process pid.
You can kill its pid to terminate the process.
In Unix/Linux, for example, if you want a program to run in the background, many of them use & to make the program run automatically at the end of the program. For example, to run mysql in the background:
/Usr/local/mysql/bin/mysqld_safe -- user = mysql &
However, joining many of our programs is not a daemon like mysqld. Maybe our program is just a common program. Generally, this program is used and ended, but if the terminal is closed, the program will also be closed. However, in order to be able to run in the background, we can use the nohup command. For example, if we have a test. php that needs to run in the background and want to run it regularly in the background, we can use the nohup command:
Nohup/root/test. php &
Tip:
[~] $ Appending output to nohup. out
Well, it proves that the program runs successfully. At the same time, it puts the output information of the program running in the nohup. out file of the current directory.
Appendix: nohup command reference
Nohup command
Purpose: run the command without hanging up.
Syntax: nohup Command [Arg...] [&]
Description: The nohup Command runs the Command specified by the Command parameter and any relevant Arg parameter, ignoring all SIGHUP signals. After logging out, run the nohup command to run the program in the background. To run the nohup command in the background, add the & (symbol indicating "and") to the end of the command.
Whether or not the output of the nohup command is redirected to the terminal, the output will be appended to the nohup. out file in the current directory. If the nohup. out file in the current directory cannot be written, the output will be redirected to the $ HOME/nohup. out file. If no file can be created or opened for append, the Command specified by the Command Parameter cannot be called. If a standard error is a terminal, write the specified command to all outputs of the standard error as the standard output and redirect it to the same file descriptor.
Exit status: This command returns the following export values:
126 you can find but cannot call the Command specified by the Command parameter.
127 The nohup Command has an error or cannot find the Command specified by the Command parameter.
Otherwise, the exit status of the nohup Command is the exit status of the Command specified by the Command parameter.
Nohup command and output file
Nohup command: If you are running a process and you think the process will not end when you exit the account, you can use the nohup command. This command can continue running the corresponding process after you exit the account/Close the terminal. Nohup means not to suspend (n ohang up ).
The command is generally in the form of nohup command &
Use the nohup command to submit a job
If you use the nohup command to submit a job, all the output of 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 preceding example, the output is redirected to the myout. file.
Use jobs to view tasks.
Disable using fg % n.
Two other ftp tools, ncftpget and ncftpput, can be used to upload and download files in the background through ftp, so that I can use these commands to upload and download files in the background.
Address: http://www.21andy.com/blog/20071121/677.html
========================================================== ========================================================== ====================
Linux Command nohup + screen
If you want to continue running the program just started after you close the ssh connection, you can use nohup. However, if you want to activate ssh the next day, you can check the status of the program running yesterday and continue working. nohup will not work at this time and you need to use screen to achieve this purpose.
A nohup
Question 1: When ssh is disabled, the program will no longer run. Why?
Culprit: SIGHUP Signal
Let's see why closing the window or disconnecting will cause the running program to die.
Linux/Unix has the following concepts:
Process group: A collection of one or more processes. Each process group has a unique process group ID, that is, the ID of the process leader process.
Session: a set of one or more process groups. It has a unique session leader ). The ID of the first process.
A single control terminal (controlling terminal) is available during the session ). The first process connecting to the control terminal is called the control process ). The process currently interacts with the terminal is called the foreground process group. Other process groups are called background process groups.
According to POSIX.1:
The default action of SIGHUP is to terminate the program.
When the terminal interface detects that the network connection is disconnected, it sends the hanging signal to the control process (the first process in the session ).
If the first process of the session ends, the signal is sent to the foreground Process Group of the session.
When a process exits and an orphan process group is generated, if any process in the orphan process group is in the STOP state, the SIGHUP and SIGCONT signals are sent to all processes in the process group.
Conclusion: when the network is disconnected or the terminal window is closed, that is, after SSH is disconnected, the control process receives the SIGHUP signal and exits, causing other processes to exit during the session period.
In short: bash and so on are all their subroutines after the ssh is enabled. Once the ssh service is disabled, the system will kill all related processes !! As a result, once ssh is disabled, the task in progress is canceled.
Example:
Let's look at an example. Open two SSH terminal windows and run one of the top commands.
[Root @ tivf09 root] # top
In another terminal window, find the top process ID is 5180, and its parent process ID is 5128, that is, log on to shell.
[Root @ tivf09 root] # ps-ef | grep top
Root 5180 5128 0 00:00:02 pts/0 top
Root 5857 3672 0 00:00:00 pts/2 grep top
Use the pstree command to see the relationship more clearly:
[Root @ tivf09 root] # pstree-H 5180 | grep top
|-Sshd-+-sshd --- bash --- top
Run the ps-xj command. You can see that the login shell (PID 5128) and top are in the same session period. shell is the first process in the session period and the PGID of the Process Group is 5128, the pgid of the top process group is 5180, which is the foreground process group.
[Root @ tivf09 root] # ps-xj | grep 5128
5126 5128 5128 5128 pts/0 5180 S 0-bash
5128 5180 5180 5128 pts/0 5180 S 0 0: 50 top
3672 18095 18094 3672 pts/2 18094 S 0 grep 5128
Close the first SSH window. In the other window, you can see that the top is also killed.
[Root @ tivf09 root] # ps-ef | grep 5128
Root 18699 3672 0 00:00:00 pts/2 grep 5128
Question 2: Why does the daemon not affect the running even if ssh is enabled?
Because their program is special, for example, after httpd-k start runs this, it does not belong to the sshd process group but a separate process group, so even if ssh is disabled, it has nothing to do with him!
[Root @ CentOS5-4 ~] # Pstree | grep http
|-Httpd
[Root @ CentOS5-4 ~] # Pstree | grep top
|-Sshd-+-sshd --- bash --- top
Conclusion: the startup command of the daemon is special and different from the General Command. For example, if a command such as mysqld_safe is used, the daemon runs. Therefore, it is impossible to transform a general program into a daemon,
Question 3: Can I get rid of ssh process group control by running commands in the background and continue running the background program?
Let's do an experiment: find/-name '* http *'&
Do I see this command and run it again after logging out with ctrl + d?
The answer is: the command is suspended !!
Because he still belongs to this ssh process group, even if it is added, he cannot get rid of it !!
[Root @ CentOS5-4 ~] # Pstree | grep find
|-Sshd-+-sshd --- bash --- find
The conclusion is: as long as it is a general command opened and executed by ssh, it is not a daemon. No matter whether it is added or not, once ssh is disabled, the system will end with SIGHUP.
Problem 4 What nohup can solve
However, in order to log out again and run it in the background, we can use the nohup command. Now we start to find/-name '* http *'&
And want to run in the background,
Use nohup: nohup find/-name "* httpd *"
By default, the output information of the program running is put in the nohup. out file of the current folder.
Adding or not does not affect this command, but it only allows the program to run on the foreground or background.
Two screens
Although nohup is easy to use, it is still relatively "simple". It can handle simple commands and is troublesome for complicated tasks that require human-computer interaction.
In fact, we can use a more powerful utility screen. Popular Linux distributions (such as Red Hat Enterprise Linux 4) usually come with screen utilities. If not, you can download them from the GNU screen official website.
1) Use
Execute screen and press any key to enter the subinterface;
I use the ping command to start execution. If I get off work, but want to disable ssh and then ping to continue running, press ctrl + a and then press d to pause the sub-interface, the [detached] is displayed. At this time, I return to the parent interface;
Use screen-ls to view the status of the current sub-interface screen-ls
There is a screen on: 22292. pts-3.free (Detached)
1 Socket in/tmp/screens/S-root, where 22292 is actually the pid Number of the subinterface;
If screen-r 22292 is returned to the sub-interface, the bullet is directed to the ping sub-interface;
2) more help
Can I use C-a (ctrl + )? To view all key bindings. common key bindings include:
C-?
Show all key binding information
C-a w
Show list of all windows
C-
Switch to the previously displayed window
C-a c
Create a new window for running shell and switch to it
C-a n
Switch to the next window
C-a p
Switch to the previous window (opposite to C-a n)
C-a 0 .. 9
Switch to window 0 .. 9
C-
Send C-a to the current window
C-a d
Temporarily disconnect screen session
C-a k
Kill current window
C-[
Enter copy/rollback Mode
Other common options:
-C file
Use the configuration file instead of the default $ HOME/. screenrc
-D |-D [pid. tty. host]
Instead of starting a new screen session, you can disconnect other running screen sessions.
-H num
Specify the buffer size for historical rollback as num rows
-List |-ls
List existing screen sessions in the format of pid. tty. host
-D-m
Start a session in disconnected Mode
-R sessionowner/[pid. tty. host]
Reconnect to a disconnected session. To connect to screen sessions of other users in multi-user mode, you must specify the sessionowner and set the setuid-root permission.
-S sessionname
Specify a name for the session when creating a screen session
-V
Display screen version information
-Wipe [match]
Same-list, but delete sessions that cannot be connected