timed actions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dirkr
Forum Newbie
Posts: 20
Joined: Sat Jul 07, 2007 2:55 pm

timed actions

Post 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 ?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

What evidence do you have that the forum isn't using a cron job?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I think there will be a cron to check that once a day.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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?
dirkr
Forum Newbie
Posts: 20
Joined: Sat Jul 07, 2007 2:55 pm

Post 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 :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Look at the source/documentation of the forum you installed.
dirkr
Forum Newbie
Posts: 20
Joined: Sat Jul 07, 2007 2:55 pm

Post by dirkr »

good idea :)
found a cron.php .. gonna have to dig thru it to see how they do it :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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
Post Reply