0

So I was curious as to if this was possible. Would it be possible to have a PHP script go to a url at a set interval, say 30 minutes?

Archey
  • 1,312
  • 5
  • 15
  • 21
  • You could just setup a script that hits the url and make a cron job for it to execute every 30 minutes. – ngen Mar 19 '12 at 17:37

5 Answers5

4

Cron jobs

Yes, just use cron jobs for calling PHP script in 30-minute intervals.

Also see my answer to another question: How to execute PHP code periodically in an automatic way

Delayed Unix call

There is also another option that should work on Unix and involves calling external script with delay without blocking the current script. It may look like this:

exec('( sleep 1800; my_php_script.php) &> /dev/null &');

although the availability of this solution depends on the system and safe_mode settings. For details about exec() function see the documentation.

Community
  • 1
  • 1
Tadeck
  • 132,510
  • 28
  • 152
  • 198
3

It would certainly be possible (you can use sleep to pause script execution for a period of time, so a while loop that does something and then sleeps will do the trick), but it would not be common to see it happen. In general, scripts in PHP and similar languages are not meant to be kept running indefinitely; you would also have to be careful not to overstep the time limit for script execution (or use set_time_limit to disable it).

Most of the time it's much more appropriate to use a cron job (for Linux) or a scheduled task (for Windows) to arrange for your program to execute every so often.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

Read about Cron. I recommend to run script using PHP interpreter from comand line, rather than wget

Grzegorz Łuszczek
  • 548
  • 1
  • 3
  • 16
0

Only, when you use a cron job. PHP scripts themselves have to be called by a user. With a cron job the php script is called in a given time frame.

http://en.wikipedia.org/wiki/Cron

adiebler
  • 135
  • 7
  • What? PHP scripts do not have to be called by the user, they can be called by eg. other scripts. Why would you think calling PHP scripts is possible only as a result of user action or cron job execution? – Tadeck Mar 19 '12 at 17:43
  • Ok, sorry. Indeed they can be called by other scripts, but they are not able to call themselves after a given time period. Sorry about any misunderstandings. – adiebler Mar 19 '12 at 17:46
  • What about `exec('( sleep 1800; my_php_script.php) &> /dev/null &');`? It orders Unix system to call `my_php_script.php` after 30 minutes, but does not wait until the system actually calls the script. This is something opposite to "_they are not able to call themselves after a given time period_". They are able to call themselves with explicit delay, without blocking execution of the current script (that could result in a timing out script execution). – Tadeck Mar 19 '12 at 18:06
0

prggmr allows for intervals to be set using the setInterval function.

Simple example

setInterval(function(){
    echo "30 minutes passed";
, 60000 * 30);

Note this is only available when running using the prggmr command or when running inside the event loop.

More information is available in the documentation.

http://prggmr.org/docs/api.html#api.setinterval

Nick
  • 763
  • 1
  • 11
  • 26