cgm225 Posted March 9, 2008 Share Posted March 9, 2008 I have a script that is used multiple times a day. However, I want to have a block of it only run every 24hrs. Has anyone written anything like this before? Could you help me code this? I am guessing I need to store a date in a flat file or database entry? Thank you all in advance! Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/ Share on other sites More sharing options...
trq Posted March 9, 2008 Share Posted March 9, 2008 You will need to setup a cron job (search the board) to run your script at a specified time. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-487779 Share on other sites More sharing options...
cgm225 Posted March 9, 2008 Author Share Posted March 9, 2008 I don't have access to setup a cron job or cron tab job.. so is there another way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-487845 Share on other sites More sharing options...
haku Posted March 9, 2008 Share Posted March 9, 2008 I don't think there is. PHP is a passive language rather than active, in that it won't do anything without some sort of input from a user (or a cron job). Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-487850 Share on other sites More sharing options...
cgm225 Posted March 9, 2008 Author Share Posted March 9, 2008 I was thinking (though I did not know exactly how to do it), that a time could be posted to a text file or database, to which a script could then reference. If the current time is >=24hrs past the posted time, then the script could run, and while doing so, update the posted time with the current time.. and so on and so fourth it would go.. Is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-487853 Share on other sites More sharing options...
haku Posted March 9, 2008 Share Posted March 9, 2008 Yes, you could do that. But the script wouldn't run at exactly the 24 hour mark, rather it would run the first time it was triggered after 24 hours had passed. If this isn't a problem, and you have no access to cron jobs, this is probably your best option. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-487859 Share on other sites More sharing options...
cgm225 Posted March 9, 2008 Author Share Posted March 9, 2008 Yes, that is what I want.. so how could I code that? (also, thanks again for the help!) Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-487861 Share on other sites More sharing options...
haku Posted March 9, 2008 Share Posted March 9, 2008 Honestly, you code it exactly how you said it. Create a column in a database that holds the time, and then choose a script (or all your scripts) and add a query at the top of them that checks the time stored in that column in the database, and if that time has been reached, launches the script (you can do this with an include function, just make sure the script has no output or the person viewing the script will be a little confused). Have the script that calls the other script then insert the current time in the database. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-488082 Share on other sites More sharing options...
marklarah Posted March 9, 2008 Share Posted March 9, 2008 If your site gets regular visits, you could just everytime a user does something on your site, make t run a checking script. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-488094 Share on other sites More sharing options...
haku Posted March 10, 2008 Share Posted March 10, 2008 That will work, although it adds a lot of extra overhead to your site. I would set it to run only on one script, but one that is used fairly regularly. That way you don't have an unnecessary call to the database every single time a person accesses a page on your site. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-488151 Share on other sites More sharing options...
eddierosenthal Posted March 10, 2008 Share Posted March 10, 2008 you need to have a piece of code that will sleep for 24 hours then wake up, do it's thing, and call itself. you should be able to call it; with 'exec' or 'system' or 'pcntl_fork' depending on your OS, and whether or not you are running apache or not. you will need to see which version of fork you need for your os and your web server. it's definitely not a database type of application you need . google for fork and php and see what you come up with. there are people on this board who will write the code for you, nice i am, not that nice. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-488197 Share on other sites More sharing options...
haku Posted March 10, 2008 Share Posted March 10, 2008 Nice or not, thats a good piece of advice. I knew about Cron jobs myself, but this is the first time I've heard of this fork. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-488205 Share on other sites More sharing options...
laffin Posted March 10, 2008 Share Posted March 10, 2008 That will work, although it adds a lot of extra overhead to your site. I would set it to run only on one script, but one that is used fairly regularly. That way you don't have an unnecessary call to the database every single time a person accesses a page on your site. this is easily avoided, by having a db entry of lastrun time, a simple check if it's been over 24 hrs (or if the date is different) than it's time to run again. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-488371 Share on other sites More sharing options...
haku Posted March 10, 2008 Share Posted March 10, 2008 That's what I was saying would add a lot of extra overhead to your site. If you have ever script querying the database every time to see if its time to run that script, then the number of unnecessary queries to the database is equal to one less than your entire queries for a day. That's why it would be better to add that check to a script that is run somewhat frequently, but not all the time. It cuts down a lot of unnecessary queries. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-488374 Share on other sites More sharing options...
litebearer Posted March 10, 2008 Share Posted March 10, 2008 a possible alternative: Presuming - 1 - the script is not hosted on your own computer 2 - you leave your home computer on 24/7 if so then simply use a scheduling program to run the script at the appointed time via your browser. I do this to gather data and generate reports each morning, so they are ready from me to read when I get up. Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-488398 Share on other sites More sharing options...
cgm225 Posted March 11, 2008 Author Share Posted March 11, 2008 My home computer is not on 24/7, and I do not have cron job/cron tab access. However, I am interested in how using fork would work. I am not asking for you to code this for me, but could you give a general piece of example code to help me understand how that would accomplish what I am looking to do? Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-489419 Share on other sites More sharing options...
redarrow Posted March 11, 2008 Share Posted March 11, 2008 read this all you need to no ok.... http://www.sitepoint.com/article/introducing-cron regards redarrow always program the safe way.......... Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-489422 Share on other sites More sharing options...
eddierosenthal Posted March 14, 2008 Share Posted March 14, 2008 here's a couple of links for explaining the ins and outs of using php for forking - i think you would have to use these hands on to get the feel of them. i've done a lot of this stuff in C when i had root, but that is a different story ... http://www.van-steenbeek.net/?q=php_pcntl_fork and http://us.php.net/exec i can't remember where i googled for this but here is a cut and paste job from an excellent article sorry i cannot give credits to author right now . please google for more. Instead of forking your PHP script you can exec() another process to run in the background. This obviously isn’t the same as forking, but it is about as close as you can get when running as an Apache module. While reading through the comments on the PHP exec() page I was able to piece together enough information to make execing a background process work. Here’s an example: exec("/usr/local/bin/myprog 2>/dev/null >&- < &- >/dev/null &”); This will execute /usr/local/bin/myprog and redirect STDOUT and STDERR to /dev/null and run the process in the background. You can even pass arguments to your program like this: $safe_arg["arg_1"] = escapeshellarg($arg_1); $safe_arg["arg_2"] = escapeshellarg($arg_2); exec("/usr/local/bin/myprog {$safe_arg["arg_1"]} {$safe_arg["arg_2"]} 2>/dev/null >&- < &- >/dev/null &”); Quote Link to comment https://forums.phpfreaks.com/topic/95229-have-script-run-some-code-every-24hrs/#findComment-492358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.