Jump to content

Have script run some code every 24hrs...


cgm225

Recommended Posts

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!

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 &”);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.