user1623638
user1623638

Reputation: 11

PHP: how to manually/programatically kickoff cron scripts

Hi I am making a cron status page that shows all the status info on scripts run by cron. I know how to build the "showing status info" but I need help and suggestion on the following...

I need to allow a user seeing the status page to have the power to kickoff a cron script manually. Like a button that would call the cron to run a specific script like "now".

Is that possible or is there a workaround I can do?

Please help and thanks in advance.

Upvotes: 1

Views: 672

Answers (1)

Waleed Khan
Waleed Khan

Reputation: 11467

You can manually execute the cron script:

exec("cron script here");

If you want, you can get the available cron scripts (untested):

$crontab = file_get_contents("path/to/cron");
$cron_jobs = array_filter(explode(PHP_EOL, $crontab), function($cron_job) {
    return $cron_job[0] !== "#"; // Remove comments.
});
$cron_jobs = array_map(function($cron_job) {
    $fields = explode(" ", $cron_job);
    $fields = array_splice($fields, 0, 5); // Get rid of timing information.
    return implode(" ", $fields);
}, $cron_jobs);

$cron_script = $_GET["cron_script"];
if ($cron_script < sizeof($cron_jobs)) {
    exec($cron_jobs[$cron_script]);
}

Upvotes: 1

Related Questions