You can use the following statement to start a PHP background process:
$ Command = "PHP script. php"; $ pid = exec ("nohup $ command>/dev/null 2> & 1 & Echo $! ");
Nohup indicates that the process is independent of the created user and can run in daemon mode.
If you need to run the background process as a Singleton, you can use the following method to record/determine whether the process is running
// Query the database for process ID $ query = "select PID from 'daemons' where 'pid '= '000000' limit 1"; $ result = mysql_query ($ query ); $ pid = mysql_result ($ result, 0, 'pid '); // check if the process is runningexec ("PS $ PID", $ pstate ); if (count ($ pstate)> = 2 )&&! Empty ($ PID) {echo "running";} else {echo "inactive ";}
You can also write the PID to a file, but it is better to store it in a database if it is in a distributed task environment.
Stop a background process:
// Check if the process from the database is runningexec ("PS $ PID", $ pstate); If (count ($ pstate)> = 2 )) {// if the process is running, kill itexec ("Kill $ PID"); // update database row with an empty process ID}
By iefreer