Reputation: 4694
Say I want a php script the run the function itstime() every hour. Other than setting up a cron job on the server how can I do this?
Or is this not possible because php scripts need to be opened to be ran?
Thanks.
EDIT: I want to write some information to a log file and depending on some variables send it in an email.
Upvotes: 1
Views: 1321
Reputation: 11211
https://www.setcronjob.com/ and various other sites, provide a web based solution for this. You can set it up to call a php script with your code in it every hour or whatever you want
<?php
itstime();
?>
Upvotes: 0
Reputation: 124355
Well, I definitely recommend the cron job for the purpose. But theoretically, you could have a PHP script run itstime()
, sleep for an hour, and loop. This is pretty crazy, though.
The script would look like:
<?php
include('whatever.php');
while(true) {
itstime();
sleep(3600);
}
?>
One would probably run it with nohup php mycrazyscript.php &
to detach it from your terminal and make it keep running, and using pkill -f mycrazyscript
to terminate it.
The reason this is crazy is that now you've got a sleeping PHP process hanging around doing nothing for 59 minutes out of every hour, and you have to make sure it's both running and running at the right time, both of which cron would take care of for you.
Upvotes: 1
Reputation: 30143
If you don't have access to cron, but do have access to a moderately busy webserver, you can use it to trigger your function every hour. This is how WordPress gets around needing cron. Here's how you might do it:
<?php
$lastrun = get_last_run_time(); // get value from database or filesystem
if( time()-$lastrun > 60*60 ) {
itstime();
set_last_run_time(time()); // write value back to database or filesystem
}
// The rest of your page
?>
Of course, there's a pretty serious race condition in this code as is. Solving that is left as an exercise to the reader.
Upvotes: 1