This article is to share the content of PHP in the Linux environment How to protect the background process, has a certain reference value, the need for friends can refer to
Application Scenarios
In some cases, we need to continue to provide some services periodically, such as monitoring memory or CPU health, these applications are not related to the client, not that the client (such as Web interface, mobile app, etc.) is closed, we do not monitor the memory or CPU, in order to deal with this business scenario, The daemon can be handy.
The environment required
Linux
Implementation method
1. Preparing the PHP script
In the/usr/local/src/directory, create a new daemon.php script file, as follows:
<?php Class daemon{/** * Initializes a daemon * @throws Exception */Public Function init () { Create a child process $pid = Pcntl_fork (); if ($pid = =-1) {throw new Exception (' Fork sub process failed '); } elseif ($pid > 0) {//parent process exits, child process becomes orphan process was adopted by process 1th, process out of terminal exit (0); }//Create a new session, disengage from terminal control, change the subprocess to the leader process $sid = Posix_setsid (); if ($sid = =-1) {throw new Exception (' Setsid fail '); }//modifies the working directory of the current process, because the child process inherits the working directory of the parent process, modifies the working directory to release the occupation of the parent process's working directory. ChDir ('/'); /** * Through the previous step, we created a new session leader, the process leader, and out of the terminal, but the conversation leader can request to reopen a terminal, in order to avoid this situation, we create a sub-process again, and exit the current process, so that the running process is no longer the session leader 。 */$pid = Pcntl_fork (); if ($pid = =-1) {throw new Exception (' Fork sub process failed '); } elseif ($pid > 0) {//once again exits the parent process, the child process becomes the final daemon exit (0); }//ByThe daemon does not use standard input and output, turns off standard input, output, error output descriptor fclose (STDIN); Fclose (STDOUT); Fclose (STDERR); }} $daemon = new Daemon (); $daemon->init (); Process business code while (true) {file_put_contents ('/usr/local/src/log.txt ', Time (). Php_eol, File_append); Sleep (5); }
The purpose of this script is to write a timestamp to the log file every 5 seconds, of course, this is just a simple example, in the application, we also need to write the specific business process code according to the different business.
2. Run PHP scripts in a future way
Under command line, enter:
Nohup php/usr/local/src/daemon.php &
3. View Log Output
Under command line, enter:
Tail-f/usr/local/src/log.txt
We will see the following information:
4. Close the PHP background process
First, we need to find out the PID of the process, command:
Ps-ef | grep "php/usr/local/src/daemon.php"
And then, through this PID, kill the process.
Kill-9 22767
Of these, 22767 is the PID number of the PHP background process.
5. Boot
through the previous steps, we know how to open and close a PHP process, however, in the actual application, we can not be manually opened every time, so we will lose some of the business data, so we have to let the process boot automatically run, the steps are as follows:
Span style= "font-family: ' Microsoft Yahei '; font-size:14px;" > In the/etc/rc.local file, add nohup php/usr/local/src/daemon.php & This command.