PHP that triggers a CRON job, or is there another option?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

PHP that triggers a CRON job, or is there another option?

Post by robster »

Hi all :)

I want to write a set of scripts, this is what I want to do and my question(s):

-Depending on a set of paramaters a script I have written needs to be run every X hours (this can range from 1 hour to 12 hours)

-When X hours is up, how can I get that script to automatically run? Is it a cron job triggered via PHP or does PHP have some kind of clock/timer functionality built in?

Toughy is this, but I'm sure one of you will have a good idea (at least one of you :)).

Thanks a load, this one has me stumped :|

Rob
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

You don't specify the script language (assume PHP) or operating system.
On linux you you simply have the cron job to run "php -f scriptname.php" or whatever...
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

if you dont want these scripts running all the time, you might want to look into at jobs to set the scripts to run when you want, and call that from the php script using an exec command.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Thank you both for your responses. I am using PHP, the testing server is OSX, the live server is Linux.
I will have a look at the AT jobs suggestion from Wayne and see what that's all about.

Basically I want a timer that runs a script when the timer counts down to 0. The time will be different each time based on other variables. Due to this the cron job might not work as I believe they are usually run at an exact time each time they are run?

I hope that clears up my question a bit more :)

Thanks again,

Rob
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

I think Cron is exactly what you need but in a different way you are thinking.

You program a script that checks your counter or other variables and this script calls the other scripts if necessary.

Now you simply call the script every hour (every minute if you need to) and you will have the functionality you need.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Now THAT was what I needed to hear :)

I awoke this morning, pencil n paper, working out all these ideas and when I login to DevNetwork Forums... you have that great reply for me. I'd like to say thanks.

Thinking out loud now, I imagine it would be something like this:

Code: Select all

-Timer is set to X hours
-Every 10 mins (for example) checker script runs via a Cron job, does a quick check on the timer script to see if timer is at 0 yet.
-If timer is at 0, do actions and reset timer to Y depending on other variables
-If timer is not at 0, do nothing

What I love about programmers (I'm an artist ;)) is they keep it simple, and what a brilliant simple solution that is. Thanks again...

Rob
fl0w
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2004 2:19 pm
Location: Stockholm Sweden

Post by fl0w »

I'm also an artist, I make myself horny when I look at my code. :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

robster wrote:

Code: Select all

-If timer is not at 0, do nothing
If timer is not at 0 decrease it by the amount of time between now and when the checker script was last called then exit actually

That way timer is set to 100 for example

then every 10 seconds checker.php is run

10 seconds are up running checker.php:
checker.php sees 100 on the timer so it decreases it by 10 and exits


20 seconds have now passed and timer is 90
checker.php sees 90 on the timer so it decreases it by 10 and exits


etc....

If it did nothing when the timer was !=0 then the timer would never decrease
Post Reply