Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill or stop execution of PHP script initiated by CRON?

Tags:

php

cron

I have scheduled a CRON which calls/executes a PHP script every five minutes. PHP script perform following tasks

  1. Checks for the flag value in database to identify if the previous run is still executing. Value of 1 in the DB tells that process is still running while a value of 0 means it is not.
  2. If the flag value is 1, then exit the PHP else continue to next step.
  3. Update the flag value in database from 0 to 1.
  4. Execute the business logic.
  5. Update the flag value back from 1 to 0, so that next CRON can executes if the data is available in user tables.

All works fine so far, depending on the size of user uploaded data the process on an average takes 35 to 40 minutes to complete.

Question, Is there anyway to kill or stop the execution of PHP script once started by cron. May be a button to let users stop the execution, upload new data and wait for CRON run. I can take care of reseting all the flags and data it's just the kill of PHP script is what i am trying to figure out.

I did some google and figured i can use some commands like:

Killall -9 PHP

to kill all php processes running on server, but not sure how to do this through PHP.

like image 488
Saurabh Avatar asked Aug 05 '13 17:08

Saurabh


People also ask

How do I stop a cron job in PHP?

Usage: ./cron. php activate to enable the cronjob and ./cron. php deactivate to disable it. The script sets the EDITOR environment variable properly (to itself) and then calls crontab -e , which on its turn calls the EDITOR (which is now the same cron.

How do you kill a PHP script?

if you need to stop a script in a windows environment, there is a taskkill command that you can run at a prompt; see computerhope.com/taskkill.htm.

How do I kill a cron job?

Stop a cron job You can stop a single cron job by removing its line from the crontab file. To do that, run the crontab -e command and then delete the line for the specific task. Alternatively, you can stop the cron job by commenting it out in the crontab file.


2 Answers

Try this:

ps aux |grep 'part_of_the_name_of_your_script'|awk '{print $2}' |xargs kill -9 {}

Or in your crontab file use crun and variable CRUN_TIME see crun -h

like image 145
Webeith Avatar answered Oct 22 '22 01:10

Webeith


You can try system() or exec(), but it might not work (or return permission denied errors) as cron processes are executed by either the current user or root, and the web server user doesn't usually have access to these processes.

like image 1
tdlive aw'sum Avatar answered Oct 21 '22 23:10

tdlive aw'sum