This article mainly introduces the example of using php to guard another php process. This article provides the implementation code directly. For more information, see
This article mainly introduces the example of using php to guard another php process. This article provides the implementation code directly. For more information, see
Use php to guard another php process (except for running the apache module and nginx)
A. php should guard B. php
In B. php, use the getmypid () function to obtain the id of the current process and write the id to the c. pid file. If the program is executed, delete or clear the c. pid file.
In. verify c. whether the pid exists and is empty. If it is not empty, read the pid and execute ps-p pid | grep file name through exec to determine whether the pid is running. Then, execute the corresponding operation.
Someone may ask why the ps aux | grep file name is not directly used. This is mainly because the file name is duplicated.
A. php code
The Code is as follows:
<?
$ Id = intval ($ argv [1]);
If (! File_exists ('pid '. $ id.'. pid ')){
Echo "not run ";
Exit;
}
$ Content = file_get_contents ('pid '. $ id.'. pid ');
If (empty ($ content )){
Echo "not run ";
Exit;
}
Exec ("ps p". $ content. '| grep B. php', $ pids );
If (count ($ pids)> 0) echo ('runing ');
Else {echo 'not run ';}
?>
B. php code
The Code is as follows:
<?
$ Id = intval ($ argv [1]);
If (empty ($ id) exit;
File_put_contents ('pid '. $ id.'. pid ', getmypid ());
While (1 ){
File_put_contents ('pid '. $ id.'. pid ', getmypid ());
Sleep (100 );
}
?>