Page 1 of 1

timed actions

Posted: Sat Jul 21, 2007 3:30 pm
by dirkr
i'm trying to have php do certain things at a specific time ( like 12am /1am/2am/.. ) every day
i know one way of doing it is a cronjob.. and have that call a phpscript.

but i wonder if there is another way in php itself?
i wonder about this cause when i look at a forum, then a forum can send out things like birthdaymails without a cronjob ( and i doubt things like that will be checked every time a user browses over the index page, cause what if nobody goes on that forum for a day ? )

anybody any other ideas besides cron ?

Posted: Sat Jul 21, 2007 3:36 pm
by superdezign
What evidence do you have that the forum isn't using a cron job?

Posted: Sat Jul 21, 2007 4:46 pm
by Ollie Saunders
I think there will be a cron to check that once a day.

Posted: Sat Jul 21, 2007 7:11 pm
by s.dot
You could do it without cronjobs, I suppose, with an infinite loop, usage of ignore_user_abort() and set_time_limit(), and some calls to scripts.

Posted: Sat Jul 21, 2007 7:29 pm
by Ollie Saunders
scottayy wrote:You could do it without cronjobs, I suppose, with an infinite loop, usage of ignore_user_abort() and set_time_limit(), and some calls to scripts.
A better option would be to invoke from the command line on system start-up from a security perspective. I'm not entirely sure how you would get a PHP script to sit idle until needed. Would a while(true) {} consume all the CPU?

Posted: Sun Jul 22, 2007 4:15 am
by dirkr
well, the reason why i think that there i no cronjob is the following:

when you install a forum ( like phpbb ) there is never anything done that makes me think it would install a cronjob.
When you install a mob ( for like birthday stuff ) you just edit a few files ( no cronjob ) and yet it works..

now either there is a way to create a cronjob with php or there is no cronjob...so i'm confused :)

Posted: Sun Jul 22, 2007 5:12 am
by Ollie Saunders
Look at the source/documentation of the forum you installed.

Posted: Sun Jul 22, 2007 6:00 am
by dirkr
good idea :)
found a cron.php .. gonna have to dig thru it to see how they do it :)

Posted: Sun Jul 22, 2007 7:03 am
by s.dot
ole wrote:
scottayy wrote:You could do it without cronjobs, I suppose, with an infinite loop, usage of ignore_user_abort() and set_time_limit(), and some calls to scripts.
A better option would be to invoke from the command line on system start-up from a security perspective. I'm not entirely sure how you would get a PHP script to sit idle until needed. Would a while(true) {} consume all the CPU?
I've written an infinite loop while writing a PHP Socket for a chat with a flash front end. Didn't have any problems with it. The script ran all the time (it was a chat server, so it had to), and no problems.

Posted: Sun Jul 22, 2007 1:00 pm
by Benjamin
Yes, using a while loop with a hardcoded true will spike cpu usage, unless you use usleep or sleep inside of it.

See the user comments for the usleep() function for more information.. http://www.php.net/manual/en/function.usleep.php