Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start/stop a cronjob using PHP?

If I type crontab -l in the command-line I can see the following line:

# * * * * * /usr/bin/php /home/user/every_minute_script.php

To start this cronjob, I need to edit the file using crontab -e command, remove the comment character at the beginning of the line, save the edited file, and exit the editor.

To stop this cronjob, the same steps, but adding the comment character at the beginning of the line.

I want to achieve exactly the same effect using a PHP script, instead of manually editing the file.

like image 250
J. Bruni Avatar asked Jul 01 '11 13:07

J. Bruni


People also ask

How do I stop a cron job running?

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.

How do you restart a cron job?

To run a cron job at every system boot, add a string called @reboot to the end of the task list. The job defined by this string runs at startup, immediately after Linux reboots. Note: Always use the full path to the job, script, or command you want to run, starting from the root.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week . 0 1 * * * - this means the cron will run always at 1 o'clock. * 1 * * * - this means the cron will run each minute when the hour is 1.


1 Answers

I did some research and found in a forum, the following message:

Call "crontab -e" with the EDITOR environment variable set to a php script. That script can modify the file and when it exits crontab will re-read the file and update.

So, I have tried something, and it worked. I will paste the working code below:

#!/usr/bin/php
<?php

$on  = "* * * * * /usr/bin/php /home/user/every_minute_script.php\n";
$off = "# * * * * * /usr/bin/php /home/user/every_minute_script.php\n";

$param    = isset( $argv[1] ) ? $argv[1] : '';
$filename = isset( $argv[2] ) ? $argv[2] : '';

if ( $param == 'activate' )
{
    shell_exec( 'export EDITOR="/home/user/cron.php on"; crontab -e' );
}
elseif( $param == 'deactivate' )
{
    shell_exec( 'export EDITOR="/home/user/cron.php off"; crontab -e' );
}
elseif( in_array( $param, array( 'on', 'off' ) ) )
{
    if ( !is_writable( $filename ) )
        exit();

    $crontab = file( $filename );
    $key = array_search( $param == 'on' ? $off : $on, $crontab );

    if ( $key === false )
        exit();

    $crontab[$key] = $param == 'on' ? $on : $off;
    sleep( 1 );
    file_put_contents( $filename, implode( '', $crontab ) );
}

exit();

?>

As it is, we have a single script named cron.php located at /home/user folder, set to be executable (chmod a+x cron.php) and called from the command-line (PHP-CLI). Later I will tweak it to run from the web, which is my intent.

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.php script) passing the temporary crontab file location as an argument. Then, the proper crontab line is found and changed, and the modified version is saved, substituting the temporary file. When the script exits, crontab will update.

This does exactly what I wanted, and fit my needs.

The other answers are nice and may fit different needs and scenarios, and I want to thank everybody who contributed.

like image 184
J. Bruni Avatar answered Sep 28 '22 17:09

J. Bruni