In linux, when running some scripts, it is often necessary to regularly run them in crontab. After a long time, there is a problem, that is, repeated running of the program consumes too much resources. how can this problem be solved? Two methods are written: the first method is to match the regular expression in linux.
Function ifrun ($ clsname, $ bf = 0)
- {
- // Perform the following Check. if a process is running, it does not run.
- $ Str = shell_exec ("/bin/ps ax>/home/root/". $ clsname. "_run.txt ");
- $ Str = shell_exec ("/bin/grep-c '". $ clsname. ". php'/home/root/". $ clsname. "_run.txt ");
If ($ bf> 0)
- {
- If ($ str >=$ bf)
- {
- Return 1;
- }
- Else
- {
- Return 0;
- }
- }
- Else
- {
- If ($ str> = 2)
- {
- Return 1;
- }
- Else
- {
- Return 0;
- }
- }
- }
-
Call: if (ifrun ('poo', 5) {die ("pooy is running");} remarks: pooy is the name of the program pooy. php! Type 2: write the process into the file, use the file function to read and then match the string.
System ('PS-ef | grep wget>/root/pooy.txt ');
- $ Arr = file ('/root/pooy.txt ');
- $ Total = count ($ arr );
- For ($ I = 0; $ I <$ total; $ I ++ ){
- $ Count = array ();
- If (stristr ($ arr [$ I], 'www/pooy ')! = FALSE ){
- // Echo '"earth" not found in string ';
- $ Count [] = 'no ';
- Break;
- }
- }
If (count ($ count)> = 1)
- {
- Echo "A same programs are running ";
- Exit ();
- } Else
- {
- Echo "start __________________________________________________";
- }
Note: "www/pooy" is a string contained in the program! Now the php program runs smoothly in linux. Summary: crontab is a good option for debugging some simple php programs in linux systems, especially php programs that require scheduled execution. it is similar to the scheduled tasks in windows, however, it is more convenient than win. we suggest you try more. |