Process daemon with PHP, process management, process error auto-start function, suitable for server administrator to use _php tutorial

Source: Internet
Author: User
Tags usleep
Just install the PHP command-line tool.

To run the example:

PHP Supervisor.php/usr/local/nginx/bin/nginx Nobody (You can also add the parameters of the corresponding command after this, without parameters left blank on the line) &


Parameter description: The first PHP executable for the command line, the second is the code file below this article, the third is the executable path that needs to be started, the fourth is what role to start it, followed by the reserved parameters, all passed to the executable program.

Advantages:

1, need to protect the program in a non-daemon state operation, error exits can be immediately restarted

2, the program for the daemon runtime, the timing of detection of operating conditions, 1 seconds to restart

3, can set the number of restarts in a certain time, the number of times more than the number of configurations will no longer restart, to prevent special circumstances of the application can not be restarted.


Suggestions:

It is recommended to run the program in a non-daemon state.


Copy the following code to save as supervisor.php file


[PHP]
Author email:tipboy@qq.com
$_retry_times=5;
$_retry_times_duration=60;//This is how much time, restart the number of times, there is no need to restart the
$_times_arr=array ();
$args =$_server[' argv '];
if (count ($args) <3)
{
Write_log ("args num error!");
Write_log ("etc:php supervisor.php/usr/local/nginx/bin/nginx nobody xxx");
Write_log ("The first parameter is this file, the second parameter is the path to the executable file to be monitored, the third is which user to execute, and the next parameter is the parameter used to execute the execution file");
Exit ();
}
$args [0] for this file name
$path = $args [1];
$username = $args [2];
for ($i =3; $i >0; $i--)
Array_shift ($args);

while (1) {
$pid =pcntl_fork ();
if ($pid ==0)
{
Write_log ("Child". Getmypid (). "Run");
Pcntl_exec ($path, $args, $_server);
Write_log ("Execute file failed");
Exit (0);
}
else if ($pid >0)
{
Write_log ("Main pid:". Getmypid (). ", Child_pid:". $pid);
$endpid =pcntl_waitpid ($pid, $status, 2);
Write_log ("Status:". $status);
if ($status ==0)
{
Description may be Daemon program, background run
if (file_exists ("/proc/"). ( $pid + 1). " /stat "))
{
Indicates that the process exists and needs to be judged regularly
Write_log ("program start Success");

for (;;)
{
if (file_exists ("/proc/"). ( $pid + 1). " /stat "))
{
Write_log ("program is Alive");
Usleep (1000000);
}
Else
{
Write_log ("program Die");
Break
}
}

}
Else
{
The description process does not exist and is not a non-daemon state.
Write_log ("program start Failed");
Exit (0);
}
}
else if ($status >0)
{
Description Non-daemon program, exit, need to restart
Write_log ("program Die");
Continue
}
Else
{
Exit (0);
}

if (Chk_need_start ($_times_arr,$_retry_times_duration,$_retry_times))
{
Continue
}
Else
{
Break
}
}
};


function Write_log ($msg)
{
Print (Date (' y-m-d h:i:s '). ' '. $msg. ' \ n ");
}

function Chk_need_start (&$_times_arr,$_retry_times_duration,$_retry_times)
{
foreach ($_times_arr as $k = $v)
{
Write_log ("duration:". $_retry_times_duration);
if ($k
{
Write_log ("Do unset");
Unset ($_times_arr[$k]);
}
}
Write_log ("Buffer count:". Count ($_times_arr));
if (count ($_times_arr) >=$_retry_times)
{

return false;
}
Else
{
$_times_arr[time ()]=1;

return true;
}
}
?>

Author email:tipboy@qq.com
$_retry_times=5;
$_retry_times_duration=60;//This is how much time, restart the number of times, there is no need to restart the
$_times_arr=array ();
$args =$_server[' argv '];
if (count ($args) <3)
{
Write_log ("args num error!");
Write_log ("etc:php supervisor.php/usr/local/nginx/bin/nginx nobody xxx");
Write_log ("The first parameter is this file, the second parameter is the path to the executable file to be monitored, the third is which user to execute, and the next parameter is the parameter used to execute the execution file");
Exit ();
}
$args [0] for this file name
$path = $args [1];
$username = $args [2];
for ($i =3; $i >0; $i--)
Array_shift ($args);

while (1) {
$pid =pcntl_fork ();
if ($pid ==0)
{
Write_log ("Child". Getmypid (). "Run");
Pcntl_exec ($path, $args, $_server);
Write_log ("Execute file failed");
Exit (0);
}
else if ($pid >0)
{
Write_log ("Main pid:". Getmypid (). ", Child_pid:". $pid);
$endpid =pcntl_waitpid ($pid, $status, 2);
Write_log ("Status:". $status);
if ($status ==0)
{
Description may be Daemon program, background run
if (file_exists ("/proc/"). ( $pid + 1). " /stat "))
{
Indicates that the process exists and needs to be judged regularly
Write_log ("program start Success");

for (;;)
{
if (file_exists ("/proc/"). ( $pid + 1). " /stat "))
{
Write_log ("program is Alive");
Usleep (1000000);
}
Else
{
Write_log ("program Die");
Break
}
}

}
Else
{
The description process does not exist and is not a non-daemon state.
Write_log ("program start Failed");
Exit (0);
}
}
else if ($status >0)
{
Description Non-daemon program, exit, need to restart
Write_log ("program Die");
Continue
}
Else
{
Exit (0);
}

if (Chk_need_start ($_times_arr,$_retry_times_duration,$_retry_times))
{
Continue
}
Else
{
Break
}
}
};


function Write_log ($msg)
{
Print (Date (' y-m-d h:i:s '). ' '. $msg. ' \ n ");
}

function Chk_need_start (&$_times_arr,$_retry_times_duration,$_retry_times)
{
foreach ($_times_arr as $k = $v)
{
Write_log ("duration:". $_retry_times_duration);
if ($k<>
{
Write_log ("Do unset");
Unset ($_times_arr[$k]);
}
}
Write_log ("Buffer count:". Count ($_times_arr));
if (count ($_times_arr) >=$_retry_times)
{

return false;
}
Else
{
$_times_arr[time ()]=1;

return true;
}
}
?>


http://www.bkjia.com/PHPjc/477518.html www.bkjia.com true http://www.bkjia.com/PHPjc/477518.html techarticle as long as the PHP command line tool is installed to run the example: PHP Supervisor.php/usr/local/nginx/bin/nginx Nobody (which can be added after the corresponding command parameters, no parameters left blank on the line) ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.