Reputation: 578
I know that for running php script every time (seconds or minute) we can use Cron (job or tab) but cron has a 60 sec granularity so we are forced to have an infinite loop for running php script . For example we can write the code below to call it at the top of the script:
#!/bin/bash
while [ true ]; do
#put php script here
done
but it's illogical because we must change php execution time in php.ini so we have a lot of problems (Security , overflow , ... ) in server . well, what should we do exactly ? My question is how to run php script every 5 seconds that hasn't got problems in php execution time .
Upvotes: 3
Views: 14220
Reputation: 21
#!/bin/sh
#
SNOOZE=5
COMMAND="/usr/bin/php /path/to/your/script.php"
LOG=/var/log/httpd/script_log.log
echo `date` "starting..." >> ${LOG} 2>&1
while true
do
${COMMAND} >> ${LOG} 2>&1
echo `date` "sleeping..." >> ${LOG} 2>&1
sleep ${SNOOZE}
done
The above script will run at a second interval.
It will not run the PHP
script when it is still processing, also reports the interaction/errors inside a log file.
Upvotes: 2
Reputation: 792
I prefere to use sleep in your PHP
while( true ) {
// function();
sleep( 5 );
}
You can stop it after 1 Minute or something like that and restart it with a cronjob. With this solution you are able to execute your script every second after your last executing and it works not asynchronously.
You save ressources with that solution, because php have not to restart all the time...
Upvotes: 0
Reputation: 1326
Use Cron job script. Get a 30 seconds interval , you could delay by 5 seconds:
-*/5-22 * * * sleep 5;your_script.php
The above script will run 5am to 10 pm
Another Alternative is,
You need to write a shell script like this that sleeps on the specified interval and schedule that to run every minute in cron:
#!/bin/sh
# Script: delay_cmd
sleep $1
shift
$*
Then schedule that to run in cron with your parameters: delay_cmd 5 mycommand parameters
Upvotes: 4
Reputation: 1370
How about using the sleep function to delay every 5 seconds
int sleep ( int $seconds ) - Delays the program execution for the given number of seconds.
Upvotes: 0