So, I'm working on a time-sensitive website in PHP on my CentOS server. I have a random time selected in the future, within 24 hours of the present. At that point, I need a PHP file to execute, and a new date to be selected and the same file to be opened. How is this possible to accomplish? I looked briefly at cronjobs, but I couldn't find a way to make them open at a specific, random, time.
Asked
Active
Viewed 1,316 times
1
-
What do you mean by a 'specific, random time'? Surely it's either specific, or it's random; not both. – Oct 13 '14 at 01:41
-
@Mike W Using php time(), I have a second within 24 hours of the present. I'd like for the script to be executed on that second. – JavaProphet Oct 13 '14 at 01:47
2 Answers
0
One possible workaround is to run a script from a cron job, say, every 10 minutes. On the top of the script, check a specific file which is supposed to contain a timestamp. If the current time is greater than the value from the file, do the job, and write the new timestamp value into this file.
$time_to_run = intval(file_get_contents('my.timestamp'));
if(time() >= $time_to_run) {
do stuff
file_put_contents(time() + random value, 'my.timestamp');
}
If you need more granularity, a better option would be to run it as a daemon (see advices here) and just loop forever (probably with some sleep()
inside) until the time comes.
-
Thing is, is that this involves money transfers in BTC. The site I'm making involves the transfer happening at a very specific time. Can I make a cronjob that runs every second? The script needs to run a DB query, and do some minor math equations, and only when needed execute some more DB querys. – JavaProphet Oct 13 '14 at 01:52
0
You can use at command, run your PHP file and in the end, register make another call to at for the next time. Something like this
<?php
// your PHP code in here, and then find out when is the next call time
$time = date('H:i', intval($time)); // or another good way to make sure time value is safe to use as a shell argument, like using escapeshellarg()
$run_me = "/usr/bin/env php " . __FILE__;
exec("echo '$run_me' | at '$time'");

farzad
- 8,775
- 6
- 32
- 41