PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 27th, 2012, 08:06 AM
mikemike27 mikemike27 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 mikemike27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 sec
Reputation Power: 0
PHP-General - 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!

Reply With Quote
  #2  
Old June 27th, 2012, 08:13 AM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,885 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 17 m 9 sec
Reputation Power: 581
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.

Reply With Quote
  #3  
Old June 27th, 2012, 08:15 AM
mikemike27 mikemike27 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 mikemike27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #4  
Old June 27th, 2012, 08:27 AM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,885 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 17 m 9 sec
Reputation Power: 581
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.

Reply With Quote
  #5  
Old June 27th, 2012, 06:07 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,944 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 16 m 54 sec
Reputation Power: 7053
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
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - What is the best method for running scheduled jobs in PHP?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap