It is suitable for server administrators to use php-written process daemon, process management, and Automatic startup of process errors.

Source: Internet
Author: User
Tags usleep

You only need to install the php Command Line tool.

Running example:

Php supervisor. php/usr/local/nginx/bin/nginx nobody (you can add the corresponding command parameters after this, leave the parameter blank )&


Parameter Introduction: the first is the php executable program of the command line, the second is the code file below this article, and the third is the executable program path to be started, the fourth is what role is used to start it, followed by reserved parameters, all passed to this executable program.

 

 

 

Advantages:

1. The program to be daemon runs in a non-daemon state, and restarts immediately when an error occurs and exits.

2. When the program is running daemon, it regularly checks the running status and restarts within 1 second.

3. You can set the number of reboots within a specific period of time. If the number of reboots exceeds the configured number, the restart will not be restarted, preventing the application from restarting in special circumstances.

 


Suggestion:

It is recommended that the program be executed in a non-daemon state.

 


Copy the following code and save it as the supervisor. php file.


[Php]
<? Php
// Author email: tipboy@qq.com
$ _ Retry_times = 5;
$ _ Retry_times_duration = 60; // you do not need to restart the instance any more than the restart time.
$ _ 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 the current file, the second parameter is the executable file path to be monitored, and the third parameter is the user to execute, the following parameter is the parameter used to execute the execution file ");
Exit ();
}
// $ Args [0] is the object 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)
{
// It may be a daemon program that runs in the background
If (file_exists ("/proc/". ($ pid + 1). "/stat "))
{
// Indicates that the process exists and must be determined 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
{
// Indicates that the process does not exist and is not in the daemon status.
Write_log ("program start failed ");
Exit (0 );
}
}
Else if ($ status> 0)
{
// It indicates that it is a non-daemon program. It exits and needs to be restarted.
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 <time ()-$ _ retry_times_duration)
{
// 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;
}
}
?>

<? Php
// Author email: tipboy@qq.com
$ _ Retry_times = 5;
$ _ Retry_times_duration = 60; // you do not need to restart the instance any more than the restart time.
$ _ 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 the current file, the second parameter is the executable file path to be monitored, and the third parameter is the user to execute, the following parameter is the parameter used to execute the execution file ");
Exit ();
}
// $ Args [0] is the object 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)
{
// It may be a daemon program that runs in the background
If (file_exists ("/proc/". ($ pid + 1). "/stat "))
{
// Indicates that the process exists and must be determined 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
{
// Indicates that the process does not exist and is not in the daemon status.
Write_log ("program start failed ");
Exit (0 );
}
}
Else if ($ status> 0)
{
// It indicates that it is a non-daemon program. It exits and needs to be restarted.
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 <time ()-$ _ retry_times_duration)
{
// 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;
}
}
?>


 

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.