0

I have a php script I need to run frequently, ie approx every 10 seconds. It seems that cron is not suitable given the required frequency.

Ideally I would like to run my script; a) in a loop with a short pause(say) 5 seconds between each execution, or b) schedule the script to run every 10 seconds

php, mysql, linux

Any suggestions are greatly appreciated.

3
  • 1
    Perhaps this can help:kevin.vanzonneveld.net/techblog/article/create_daemons_in_php Commented Feb 9, 2012 at 4:35
  • @vucetica: when I clicked to upvote your answer, it was not there anymore! Commented Feb 9, 2012 at 4:37
  • 1
    yes, someone commented that it is more appropriate to be a comment and I agree. Commented Feb 9, 2012 at 4:49

4 Answers 4

2

Set an every minute cronjob to run your PHP script.

In your PHP script, run the code in a loop, 6 times.

You can use sleep function to create a pause between each loop execution: http://php.net/sleep

Sign up to request clarification or add additional context in comments.

Comments

0

You could set cron to run every minute to initialize the script, then create a loop that checks the date("s"); if it's lower than 51 then repeat the loop else exit the loop so you don't generate an infinite loop. ALthough you need to keep in mind that this is extremely inefficient as it will hold php running non stop and may make your server unresponsive to normal means. There are other more efficient schedulers out there that can run on a shorter timespan. One thing that I've done is to create a c/c++ daemon that runs on the box and just hits the php app once every 10 seconds. Although this is much more efficient and allows the php script to exit, you have to add error checks to make sure your server responded or actually got the ping at the 10 second interval, for instance if it's running under apache that may pool connections on an extremely busy server.

Comments

0

Use a shell script that runs in an infinate loop. This script should trigger your PHP script that does its stuff once (and only once) then quits.

You can put a delay between the running with the sleep command.

This is done so that PHP exits after each run, so that PHP can free up memory and CPU time.

Comments

0

In cron you can set the script to execute in every 10 seconds . I afraid , how much time your script will takes to execute in that case.

Another option is you can create an infinite loop in a program , then task to be done will be inside the loop block. after executing the loop once, put a delay of 10 seconds using either sleep or usleep. Here you dnt have much control over the program. If you want to stop the program, needs to use linux kill commands to terminate the task .

2 Comments

No, in cron you can't set the script to execute in every 10 seconds. AFAIK, the minimum interval is 1 minute.
If so, you need to run the script continuously and put a sleep between each iteration . Infinite loop with delay will help .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.