0

I'm invoking some PHP backend scripts on Apache webserver that are programmed to run for a few days. The trouble is that I cannot figure out a way to stop these scripts (except restarting the server using sudo apachectl restart).

Normally I would run ps -aux | grep script_name get the PID and kill it.

But in this case when I do this using PHP ( shell_exec("ps -aux | grep script_name") I do not see the process.. my hypothesis is that when invoking the scripts via apache it runs them internally, so they are not visible.

Any ideas how to check which scripts are running and abort these when needed?

  • Have you considered coding your scripts to utilize PHP's set_time_limit()? If you know a hard limit to set, that could be one method. Or just locate them in the process table and kill them. – Jim L. Jan 8 at 19:08
  • I actually set the limit to a few days.. but sometimes I wish to abort the action, but I cannot figure out how to kill these processes (except for restarting apache)... – Roy Granit Jan 8 at 21:44
  • Traditionally, the kill command is used to kill a specific process by referencing its PID number. The ps or pgrep utilities may help in determining which specific PID matches the process you want to kill. Do they not work for you? If not, please edit your question to say what difficulties you are having when you try to kill the PHP process you want to kill. – Jim L. Jan 8 at 23:15
  • I've edited the question to provide additional details. Indeed my plan was to use ps and kill but they do not seem to be working in this context for some reason (see above) – Roy Granit Jan 9 at 6:57
  • How do you start the scripts? Are they started from within apache as a result of opeing a page or from within another script and then forked? – Christian Wolf Jan 9 at 14:00
0

OK, from the info I got from your comments, I think you are useing the mod_php apache module to include the php scripts. This also explains why you have to restart apache which in terms will kill all threads including the php runners.

As the php is running as a thread in your apache you will have no chance in using kill or the like because the php parser is not forked or an individual process at all.

The most clean soluion (from my perspective) is to regularly check for a flag been set. I assume that your script does some computational heavy things that cause the delay. You can split the computational effort into smaller chunks. If it is IO related stuff that is causing the delays, you might need to split that up or set timeouts.

Then you check in regular intervals if a certain flag has been set (let it be a DB setting, a folder be generated or the like). If yes, the user requested the abortion and you can even gracefully stop the computation.

All you need to do now is to define an additional script that sets the flag. Do not forget to remove the flag once the termination of the main worker has been stopped and things are tidy again.

|improve this answer|||||
  • Thanks. I ended up using this "naive" approach and it did the trick – Roy Granit Jan 25 at 21:20
1

Running things which take time within a webrequest is just plain wrong - even in 2020 with http/2.

It is possible to achieve the result of identifying and terminating the relevant process from the command line (hint mod_status) but its the XY problem.

See - https://stackoverflow.com/questions/2212635/best-way-to-manage-long-running-php-script

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.