#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    What is the best method for running scheduled jobs in PHP?


    I have a webpage form that requires a date and time to be submitted. When it's submitted I need the back-end to run a script at the time and date specified.

    Have you ever needed to do something like this? I've tried the exec() function with an at command inside. It won't work. I have no idea why and there doesn't seem to be a lot of advice on google about this. so either no one has ever had a problem with it before or no one has ever tried because there is a much simplier way.

    I've also tried a shell script but I didn't really understand what I was typing. It was mostly a guess. Here it is;

    Code:
    $fileOutput = "#!/bin/bash\n\n";
    $fileOutput.= "php ".$doc_root."/byot/author/publish_tables.php jobid=".$jobid." ".$result." catid=".$catid." docroot=".$doc_root;
    
    $myFile = $doc_root."/byot/author/publishjob".$jobid.".sh";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $fileOutput);
    fclose($fh);
    
    shell_exec('chmod a+x '.$myFile);
    
    $fileOutput = "#!/bin/bash\n\n";
    $fileOutput.= "at $publishTime $publishday.$publishmonth.$publishyear -f $myFile";
    
    $runfile = $doc_root."/byot/author/runat".$jobid.".sh";
    $fh = fopen($runfile, 'w') or die("can't open file");
    fwrite($fh, $fileOutput);
    fclose($fh);
    
    shell_exec('chmod a+x '.$runfile);
    exec($runfile);
    Any tips or advice greatly appreciated!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    If I understand what you are asking, I think you want to use the 'at' command from your PHP script.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by gw1500se
    If I understand what you are asking, I think you want to use the 'at' command from your PHP script.
    Yes, ideally. But when I put that in to an exec() it just doens't work. It doesn't come up with any kind of error, it just doesn't run.

    Ideas?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    The first step is to run the 'at' command from a command prompt as the user running httpd (e.g. 'sudo -u apache at ...'). There are some config issues at the OS level you need to set up to even use 'at'. Once you know 'at' works for that user, then you can debug the PHP end.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    I recommend setting up a batch/queue type structure to handle this.

    Set up a database table that contains the tasks you need to run at a later date.

    Then create a PHP script that reads the database table, fetches current tasks, and then executes them.

    Use a CRON job to execute the PHP script frequently.

    The advantages of this system are that it can survive a server restart / other interruption to your job handler and you also have a record of which jobs have/will run.

    Comments on this post

    • aeternus agrees : great idea! this also prevents double execution, awesome
    • nocturns2 agrees : You can also add which jobs finished successfully. Great post.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo