1

Is it possible to setup a cron job from within a PHP script on a LAMP server to run another PHP script? I'm a newb with cron jobs but I do know php quite well. Do I have to access the shell from PHP to set up the cron job? How would I code that if, for example, I want to execute execute.php automatically in 5 minutes after it is called from call.php?

Edit: Just to be clear, I only want to run execute.php once 5 minutes after it is called, and not have it repeat daily or anything like that. So, after it is executed by the cron job, the cron job should be discarded.

5
  • I have tried to search up ways to create a cron job, but I couldn't find info on how to set it up from PHP. I don't want to set it up manually, it needs to be automatically setup from a PHP script. I couldn't really find info on that, and I don't really know much about cron jobs other than the fact that they run scheduled jobs. Commented Aug 9, 2012 at 16:01
  • 1
    So you want to run a script then AFTER it finishes, leave a 5min gap before doing it again. Rather that every 5mins? is this to stop it overlapping, if its execution is too long? A scenario would help here. Commented Aug 9, 2012 at 16:02
  • @Waygood see the edit I added to my post, I just want it to run once, exactly 5 minutes after it is called, and never again, unless it is called again from execute.php. So, if user visits call.php, execute.php should run exactly 5 minutes later even if user leaves page, closes browser, etc. After that execute.php doesn't run again unless it is called by call.php again, in which case another schedule would be created. Commented Aug 9, 2012 at 16:05
  • 1
    You could fire off the at command in a shell when your trigger script is done running - computerhope.com/unix/uat.htm Commented Aug 9, 2012 at 16:06
  • I take it that you or a user will start the call.php script running and then execute.php will need to be done 5 mins after. What if call.php is run again 1 second after another? A better explanation of what theses script are doing would help, and allow us to better plan for clashes, over-runs etc.. BUT @Crontab suggestion of 'at' through exec() would be the way forward so far. Commented Aug 9, 2012 at 16:23

2 Answers 2

2

Cron doesn't work exactly like that, but you can set something up to create the functionality you want.

I would first set up a cron entry to execute execute.php every minute.

Then, when call.php is run, call.php makes an entry in a database table or flat file with the time that execute.php should be called.

When the cron entry is run, execute checks the database table or flat file to see if it's supposed to run the code in the file at that time, and if it is, runs it.

Sign up to request clarification or add additional context in comments.

Comments

1

Use sleep at the beginning of execute.php

sleep(5*60);
//Rest of the code

It should by called like this:

require_once("execute.php");

However, call.php will not send any response for 5 minutes

6 Comments

Would this run execute.php even if user leaves the page before sleep timer expires?
Now that I think about it, it would only work with ajax. If you are new at programming, this would be rather difficult
I'm familiar with AJAX and JavaScript, I'm experienced in web programming, but not so much with cron jobs.
then this is OK, if you call "call.php" from ajax, it will wait 5 minutes and then run the script, even if the user leaves the page. It's only useful, tough, if you use ajax
Oh, I see, so I use AJAX so it runs in the background so it doesn't look like the page is unresponsive? I think I get what I should do now. Thanks, this is waaay simpler than a cron job.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.