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 .
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
#!/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.
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.
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...
Related
i have a script in php extract_data.phpand it takes 20 minutes to run the script.
I activated cron to run the script. but the cron has a time of only 30 seconds (this time cannot be increased).
the problem is that i always get the timeout error.
I would like it to appear: file loading... while the script is running.
i test exec("extract_data.php"." > /dev/null &");
does not work
It seems that you have a default configuration for the timeout.
Please take a look here: https://www.php.net/manual/en/function.set-time-limit.php
set_time_limit(0); // To run without timeout.
Or
set_time_limit(20*60); //to allow to run for only 20 minutes
I can create a bash script that runs a PHP file over and over with a 3 second delay like this:
php -q /home/script.php
sleep 3
php -q /home/script.php
sleep 3
php -q /home/script.php
But I'm looking for a better way to do this, so I don't have to create a file with hundreds of thousands of lines and then check to see when it's done so I can just restart it.
Is there any way to create a loop that runs a PHP file and once it's done, it just does it again - for an infinite amount of time (with a 3 second delay between each run)?
Using Cronjob
What Are Cron Jobs? https://www.hostgator.com/help/article/what-are-cron-jobs
A cron job is the scheduled task itself. Cron jobs can be very useful to automate repetitive tasks.
Some useful tool for cronjobs: https://crontab.guru/
An example: "run a script every 1 minute"
*/1 * * * * bin/php /path/to-your/script.php
Looping
In case you really need to repeat a task each X seconds, you could write a while loop for that:
#!/bin/bash
while true; do
# Do something
sleep 3;
done
You may consider writing a while loop:
while true
do
php -q /home/script.php
sleep 3
done
Any idea why a simple infinite loop stops on the very first minute of every hour? For instance, 21:01, 22:01, 23:01, etc.
The server is running Ubuntu 12.04 and the PHP script is launched using command: "php -f test.php"
while (1 == 1) {
echo "test";
sleep(30);
}
Any help is appreciated.
UPDATE: It doesn't matter whether I run the script 16:05 or 16:49, it will stop on 17:01, so the issue is not related to the set_time_limit value.
UPDATE: If the script has to sleep for an hour and then echo "test", the "test" will not be echoed. The script stops it's work without finishing the loop itself.
UPDATE: It seems that I've found out what's killing the script. I've set the PHP script to report All errors and right before the *:01 time comes up, I get the text: Terminated. I've Googled that the script might get Terminated by OOM killer and, unfortunately, I don't have permission to change it's settings on my current VPS. I'm switching to VDS and will try to modify OOM killer settings.
i guess it's because of Limits the maximum execution time on php .
if you want repeat a script every day , hour and minute you should use Cronjob to call your script every time you need.
cronjob available in popular web hosts like direct admin and cpanel
for example you can use below command for run your script every one minute
*/1 * * * * /usr/local/bin/php -q /home/user/domains/domain.com/public_html/script.php
if you want run script every second you can set maximum execution to one minute and repeat a code every second in it and use cronjob to call it every minute
If you are using php file through apache i.e. as a URL then you should do
ini_set('max_execution_time', 0); //0=NOLIMIT
If you are running through CLI this should not be happening generally
UPDATE
Below are few Notes form php.net. I hope it will help you
Warning This function has no effect when PHP is running in safe mode.
There is no workaround other than turning off safe mode or changing
the time limit in the php.ini.
Note: The set_time_limit() function and the configuration directive
max_execution_time only affect the execution time of the script
itself. Any time spent on activity that happens outside the execution
of the script such as system calls using system(), stream operations,
database queries, etc. is not included when determining the maximum
time that the script has been running. This is not true on Windows
where the measured time is real.
So, it is possible that PHP code doesn't actually breaks. But, any other conenction/call is getting broken.
On my ubuntu system there is a script in /etc/cron.d/php5
# /etc/cron.d/php5: crontab fragment for php5
# This purges session files older than X, where X is defined in seconds
# as the largest value of session.gc_maxlifetime from all your php.ini
# files, or 24 minutes if not defined. See /usr/lib/php5/maxlifetime
# Look for and purge old sessions every 30 minutes
09,39 * * * * root [ -x /usr/lib/php5/maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/php5 ] && /usr/lib/php5/sessionclean /var/lib/php5 $(/usr/lib/php5/maxlifetime)
That script /usr/lib/php5/maxliftime will check for param
session.gc_maxlifetime
in any php.ini file located at /etc/php5/*/php.ini
Check for that value via
cd /etc/php5
grep -ri gc_maxlifetime .
If there is a value 3600 you should got it.
I am calling a shell script in Linux using a PHP script, I do the following:
shell_exec('./shell_script.sh');
after this the PHP script continues on.. All this works as expected.
However, sometimes the shell_script doesn't finish executing for whatever reason, so here is the question:
How can I terminate the shell_script.sh after being executed for x amount of time?
Should this be dealt with in PHP itself somehow (dont think that's possible in this instance) or should it be done in the .sh itself?
So just after the:
#!/bin/bash
at the beginning of the .sh, is there something I can put for it to terminate if execution time exceeds say 20 seconds perhaps?
I don't know if you can do it in php, and there may exist better solutions in bash, but this is what you could do:
This is the line you can put immediately after #!/bin/bash to kill the current script after 20 seconds:
(sleep 20 && kill $$) &
bash replaces $$ with the pid of the current script.
You could use something like this:
# start timer
( sleep $TIMEOUT ; kill $$ ) 2>/dev/null &
TIMER_PID=$!
# "payload"
echo hello
# cancel timer
kill $TIMER_PID
YMMV though, in my tests the part that is supposed to cancel the timer sometimes doesn't kill sleep and the program waits until the timeout finishes.
I am having trouble executing at command via php. When I start this job, it runs the 1st time and creates a queue job 1 minute from it finishes the Run Logic. The queued job runs at specified time but it will not create a new job queue 1 minute after it completes the Run Logic so it dies.
The pseudo logic is
//task_queue.php
if(we_have_tasks){
Run Logic
shell_exec('/usr/local/bin/php -f task_queue.php | at now + 1 minutes');
}
What am I doing wrong? Any help is appreciated. I thought if I used the 'at' command I'll be able to schedule jobs without overlapping.
Thanks
I think you meant to write shell_exec('echo "/usr/local/bin/php -f task_queue.php" | at now + 1 minutes'); no? The version you've got re-runs immediately.
You can try
exec('php /usr/local/bin/php -f task_queue.php | at now + 1 minutes');