Recently, I wrote a program to monitor online processes. it aims to monitor the disconnection of execution programs in real time and restart them in time. It's not difficult to write in PHP, but it's quite a sense of accomplishment for myself. Procedure
Recently I wrote a program to monitor online processes.
The purpose is to monitor the program disconnection in real time and restart it in time.
It's not difficult to write in PHP, but it's quite a sense of accomplishment for myself.
The procedure is omitted.
# Point the perl process to a text. $ Psfile
$ Command = "ps ax | grep perl | grep-v \"> $ psfile \ "> $ psfile ";
Exec ($ command );
$ Process_array = file ($ psfile); # open the text
$ Sum_process = sizeof ($ process_array); # each row of the text is used as an array element. this sizeof is the number of elements, that is, the number of rows.
For ($ I = 0; $ I <$ sum_process; $ I ++)
{
Echo $ process_array [$ I]. "\ n"; # $ process_array [$ I] indicates the record of line I in the text
Echo $ province_id [$ PID]. "\ n ";
If (strstr ($ process_array [$ I], $ province_id [$ PID]) # compare whether a process exists
{
Echo "processes \ n ";
$ Thread = 1;
Break;
}
}
$ Command = "perl". $ program. ">". $ phppath. $ name. "abc.txt &"; # restart the process command
System ($ command); # restart the process
Note:
1. command ps ax | grep perl | grep-v "> $ psfile"> $ psfile
Strange: ps ax | grep perl> abc. dat (correct command) execution in sh is different from execution in PHP, so you need to add a pipeline | grep-v "> $ psfile. The general function is that-v does not match $ psfile and other outputs. The specific reason is not clear.
2. system () function
PHP has a problem with this function ~ During execution, the program jumps out after the current process program is executed. it continues to loop through the next program to be restarted, that is, if multiple processes need to be restarted, this function can only be restarted after the current process program is executed ............ (Shame)
Solution: write the command and run it in the background. Is --- add one more & (don't underestimate this ~ Can solve the big problem)
For example, perl abc. pl> abc. dat &
OK ~
Success! ~ ^-^ ~