Jump to content

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


mikemike27

Recommended Posts

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;

 

$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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.