[PHP] process

Source: Internet
Author: User
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! ~ ^-^ ~

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.