nohup runs php scripts permanently in the background
What is a nohup
nohup is a command in Linux and Unix that allows a process to continue running in the background when the terminal exits. Its full name is "no hang up", which means "not to hang up". The nohup command allows you to continue running the command after exiting the terminal or closing the SSH connection.
nohup syntax rules
The basic syntax of the nohup command is as follows:
1 |
nohup COMMAND [ARGS ...] [> output-file 2> error-file] & |
The meanings of the parameters are as follows:
COMMAND
: Commands or scripts that need to be run in the background.ARGS
: Specifies the parameters of a command or script.> output-file
: Output redirects to the specified file.2> error-file
: Error messages are redirected to the specified file.&
: Puts the command in the background.
The nohup command is executed in the following steps:
- The nohup command redirects the current shell's standard input, standard output, and standard error output to
/dev/null
In the device, avoid being interrupted by the signal that shuts down the terminal. - The nohup command executes the process in the background and outputs the PID of the process to the terminal.
- The process starts to execute and redirects the standard output and standard error output to the specified file.
- The user can exit the terminal or close the terminal window, and the process still runs in the background.
At work, we often run a very important program, sometimes this program needs to run for several hours, or even several days, at this time if we exit the terminal, or the network is not good and the connection is interrupted, then the program will be suspended. And this is definitely not what we want to see, we want to even if the terminal is closed, the program can still run.
To make a PHP program execute in the Linux background, use the nohup command with the & symbol. The nohup command allows the process to continue running after the user has logged out, & you can put the process in the background to execute.
The nohup command is an abbreviation of the English phrase no hangup, which means not to hangup, meaning that the program does not exit. This command will cause the program to ignore the HUP signal and ensure that the program can proceed normally. The HUP signal, which some people may be unfamiliar with, is a signal sent to the process associated with it when the terminal is suspended, and the process will stop running after receiving this signal. So if you don't want progress to be killed by this signal, you can ignore it. And that's what nohup ordered to do.
php programs run in the linux background
Command: nohup php socket.php &
In this way, the terminal will not be disconnected and a nohup.out file will be generated in the project directory, recording all information and exceptions to close using:
ps aux | grep php netstat -anp | grep (port number)
Look at the PID and use: kill (PID)
1. Use nohup to run php scripts permanently in the background:
nohup php -f /www/wwwroot/default/redis_subscribe.php &
2. Check the process:
ps -ef|grep redis_subscribe.php
3. View all processes:
ps -ef
4. View a process:
ps [PID]
5. Terminate the process:
kill -9 [PID]
Common method of the nohup command
Start a program using the nohup command
If you need to run a program that keeps running even after the corresponding Shell is exited, you can run the program using nohup like this:
$ nohup command
When the program is running, the log output of the program and its error log will be recorded in the nohup.out file, which is usually located in the home directory or the current directory.
Redirects the program's output
What if I don't want to save the output of the program in my home directory or in the current directory, I want to save it in the path I specify, and I want to customize the file name? At this point we can use the redirect action >.
For example, I now have a script called myScript.sh and I want to save its output in the output directory of my home directory called myOutput.txt and run it like this:
$ nohup ./myScript.sh > ~/output/myOutput.txt
Start a program in the background using the nohup command
If you want the program to run in the background, you can add an ampersand. But when you do that, the program disappears. To get the program back to the terminal, use the fg command.
The output log of this command will be saved in the nohup.out file, which you can view using cat or other commands. The number 8699 in the second line represents the process number of the command, which is the pid. We can use the ps command to find this process.
Use nohup to run multiple programs simultaneously
If you need to run multiple programs at the same time, it is not necessary to run one by one, just use the && symbol. For example, if you want to run mkdir, ping, ls at the same time, you can run it like this:
$ nohup bash -c 'mkdir files &&ping -c 1 baidu.com && ls'> output.txt
Terminates the process running in the background
As mentioned above, the nohup command combined with the & symbol allows processes to run in the background, even if the terminal is shut down. Now, if you want to stop this process, what do you do?
The simplest is the kill command, which I believe you have used many times.
$ kill -9 PID
So how do you find the pid of the process? We can use the ps command.
$ ps aux | grep myScript.sh
Or you can use the pgrep command.
Next, use the kill command to terminate the process.
$ kill -9 14942
Sum up
This article about nohup running php scripts in the linux background is introduced to this, more related php programs in the linux background execution content please search the script home previous articles or continue to browse the following related articles hope that you will support the script home in the future!
Related article
-
Web game development introduction Tutorial three (Simple application)
It doesn't really matter which combination you use. What matters is time and cost. The complexity lies in the interaction and refinement of the data, not in the implementation of the technique or effect.2009-11-11 -
php verification ID number correctness function
This article mainly shares two paragraphs of php to verify whether the ID card number is correct function, interested partners can refer to it2016-07-07 -
The Laravel framework implements an example of CURD operations at the model layer
This article mainly introduces the Laravel framework to implement the model layer of the CURD operation, combined with the form of examples to analyze the Laravel framework model layer of the database of the CURD operation specific implementation skills, the need of friends can refer to the next2018-05-05 -
Examples of the use of assets in YII
This article mainly introduces the use of assets in YII examples, need friends can refer to it2014-07-07 -
PHP code to get remote web content (fopen,curl tested)
Get remote web content of the php code, do thief collection program program often used, now use curl more.2011-06-06 -
PHP magento background login problem solution
This article mainly introduces the PHP magento background can not log in to solve the problem of relevant information, here magento can not log in, provide two solutions, need friends can refer to2016-11-11 -
PHP namespace (namespace) principle and usage details
This article mainly introduces the PHP namespace (namespace) principle and usage, combined with examples of the form of detailed analysis of the concept of PHP namespace, principle, usage and related operation precautions, need friends can refer to2019-12-12 -
php send sample mail with attachment
php send mail, there will be a lot of related articles on the Internet, and this article introduces you to send mail with attachments, friends who do not know can refer to it2014-01-01 -
The DEDECMS home page calls multiple images from the gallery
This article is to share with you the method of calling multiple pictures in the photo collection on the home page of the dream weaving system, and those who have the same needs can refer to it.2015-06-06 -
A few words about PHP object-oriented identity objects
The following Xiaobian will bring you a brief talk about PHP object-oriented identity objects. Xiaobian feel very good, now to share with you, but also to give you a reference. Let's take a look2017-06-06
Latest comments