3

how can I execute a php script from server every 30 seconds?

I have a php script and I need to execute it every 30 seconds to retrieve some information from other sites by curl.

but I dont know to run a php page in background by apache every 30 seconds.

Salgar
  • 7,687
  • 1
  • 25
  • 39
Hamidreza shaabani
  • 165
  • 1
  • 3
  • 10

2 Answers2

6

Use Cron job script. Get a 30 seconds interval , you could delay by 30 seconds:

-*/5-22 * * * sleep 30;your_script.php

The above script will run 5am to 10 pm

Sudhanshu Saxena
  • 1,199
  • 12
  • 32
  • Using a solution like this, is not straight forward as Cron does not split its self into sub-minutes.. So this answer seems the best viable solution, you might have to conduct further tests into the stability of a solution like this. – Daryl Gill Jun 10 '13 at 13:12
  • 1
    the main disadvantage of this script is if the current script complete the execution in 20 seconds then the next cycle will be 50 as the timer didnt count the execution time. – Sudhanshu Saxena Jun 10 '13 at 13:20
  • So the OP will have to either sleep the PHP script for a few moments, or find another way to compensate this – Daryl Gill Jun 10 '13 at 13:26
  • The another method is if the script load take more time then conditional script also inserted.This can be override by conditional calls.In this case use two scripts. – Sudhanshu Saxena Jun 11 '13 at 05:05
2

A cron run every minute. You should think about if one minute is enaugh.

Running a cron every 30 seconds

When you want to execute your script more often you need a deamon which run your script in a specified time. But its more complicated as a cronjob.

http://kvz.io/blog/2009/01/09/create-daemons-in-php/

Community
  • 1
  • 1
René Höhle
  • 26,716
  • 22
  • 73
  • 82