1

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.

1 Answers1

1

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]);
}
Waleed Khan
  • 11,426
  • 6
  • 39
  • 70
  • Hi, thanks for your help. What I want to know is, does exec() function call cause the script to run separately from the browser? What I need to know is, would calling exec() make the cron script I want to run manually make the browser keep loading for a long time? Or calling exec() be asynchronous from the browser. Tnx. – user1623638 Aug 27 '12 at 14:12
  • @user1623638 Calling `exec` from PHP can be asynchronous (you can probably append a `&` to it), but if you do so, you won't be able to see the results. – Waleed Khan Aug 27 '12 at 14:13
  • Actually, asychronous call is exactly what I need b/c the scripts are very long process and the browser would timeout. I would output results to a database entry. Would using the appended & allow the browser to finish loading and allow the script to run for hours? tnx. – user1623638 Aug 27 '12 at 14:49
  • @user1623638 It doesn't seem so. Try http://stackoverflow.com/questions/45953/php-execute-a-background-process – Waleed Khan Aug 27 '12 at 15:09
  • I read that post, doesn't help in what I really need. I really need to be able to manually run long cron scripts from the browser and not have the browser timeout but to just spit out a text "This cron script has started" and thats it and have the cron script do its thing separately. Any ideas or workarounds? – user1623638 Aug 27 '12 at 15:37
  • @user1623638 That link's proposals seemed perfectly fine and usable to me. – Waleed Khan Aug 27 '12 at 15:40
  • doesn't seem to help me. Anyone else can help in my situation? – user1623638 Aug 27 '12 at 21:38
  • @user1623638 You may want to just open a new question. – Waleed Khan Aug 27 '12 at 23:00