1

I have this code:

<?PHP
ignore_user_abort(true);
set_time_limit (0);

while(1) {

    // my codes

}
?>

now After a while how to detect that script is running yet or not?

I used this role for this question:

<?PHP
ignore_user_abort(true);
set_time_limit (0);

while(1) {

    // my codes

    $statusfile = strtolower(file_get_contents('status.txt')); // for stop checking
    if (!(strpos($statusfile,'stop') !== false)) break;

    $status = /* my status */; // for writing status of itself
    file_put_contents('status.txt', $status);

}
?>

but in this role my script itself must write status of itself and if Suddenly stopped (such as server reset or occuring an error in script) will not write any status and we cant detect status of it such as is stopped or runnig.

now I want know that how can I detect runnig status of a script from out?

and how can I stop it from out?

Hamidreza shaabani
  • 165
  • 1
  • 3
  • 10
  • 1
    the instance of php will run in the proces tree so `ps aux | grep php` get it PID and then just do `kill -9 pid` (thats assuming the script runs as a console job) if not then you just need to do `ps aux | grep apache` (assuming apache is your web server) look for the process thats got a long runtime (you can also use `top` to find it) get the PID from that list and repeat the kill command using the new PID – Dave Jul 18 '13 at 11:06
  • please explain more. whats **`ps aux | grep php`** and how can I reach it? – Hamidreza shaabani Jul 18 '13 at 11:10
  • 1
    **This is for UNIX users only** `ps aux | grep php` will list all running processes which _are_ instances of `php`. If you're script is called `myscript.php`, then you should see `php /path/to/myscript.php` in this list when it's running. – John WH Smith Jul 18 '13 at 11:14
  • If you're running on windows you can't (unless you can pull up task manager manually) and if you only have web browser access to the server and you're running the script via the web server again you can't kill the process. Either way running an infinite loop script without the ability to kill it on command is a bad idea. I use a text file that contains a 1 if I want it to run and I just simply edit and save it to a 0 when I want it to die. Each itteration of the loop I check the text file see whats in it. – Dave Jul 18 '13 at 11:28

0 Answers0