PHP Exec and at command - php

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');

Related

Bash script to automatically re-run PHP file?

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

Activate Cron job from a PHP page

I googled but didn't found any solution
I have a PHP page that takes 45 minutes to execute.
What I am trying to achieve is:
1. Whenever I run a URL abc.com/test.php the script should check the cron job and activate it (run myscript.php) .
2. And should execute the page until it's complete.
So the PHP should call a cron job and execute it ONLY once when it is requested.Is Cron right approach
I do not want the script below, which i tried This will add a script that runs every day at 9:30am.
exec('echo -e "`crontab -l`\n30 9 * * * /path/to/script" | crontab -');
Why set a new cronjob, if you only want to execute it once?
exec('php -f /path/to/script >> /dev/null 2>&1 &');
This will run the script. Echo all the output into the nowhere and use fork, so it will run in the background and your Request won't wait for a return.

run php script every 5 seconds

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...

Cron job runs 2 times and then stops

I m trying to configure a cron job via cpanel. What i want to do is execute a php script that updates a value in a mysql database table every minute. i tryed 2 cron commands :
wget -O – -q "URL_HERE"
and
usr/local/bin/php -q /home/user/public_html/filename.php
note that its a very small script with tiny execution time, so it should be ok.
for testing purposes I configured it to run every minute to see if it works.
Here is the problem:
the cron job runs ok for the first two times , but then it stops. for example if the time is 12:00, the script will run on 12:01, then on 12:02, but NOT on 12:03 .. 12:04 and so on. What could be the problem here?
thanks in advance
Make sure your crontab entry is correct...
For running a cron every minute, one needs to add entry something like
* * * * * "username" "task to execute"

Unix 'at' command and returning a job number

I'm currently using the unix at command to schedule jobs in the future. However, I also want to be able to delete these jobs if I deem it necessary. Is there any way to get the job number immediately after the creation of a job? I know that atq can show a list of pending jobs but I want to be able to get the job number in PHP code immediately after creating the job. I can run the atq command but what if another job is created before I get the last job in the queue? Then I don't have the correct job number.
Long story short, is there any way after creating a job with the at command that I can get the job number in return without using the atq command?
For example, my code is:
exec('echo /usr/bin/php -f /home/site/public_html/test.php | sudo /usr/bin/at now');
I'm currently looking over command substitution that one comment suggested to figure out how to get the job number from this php exec() code.
When I try
var=$(echo /usr/bin/php -f /home/site/public_html/test.php | sudo /usr/bin/at now)
in the shell and echo $var I get an empty line. Why doesn't var hold the output?
$x=exec("echo sleep|at 14:00 2>&1");
$match="";
if(preg_match("/^job\s*(\d+)\s*/",$x,$match)){
$job= $match[1];
print "job: ". $job . "\n";
}
Try:
exec('at now "php /home/site/public_html/test.php" 2>&1 | grep job | awk "{print $2}", $output);
var_dump($output);
Help from:
https://superuser.com/questions/368328/have-the-at-command-return-the-job-id-of-the-task-just-submitted

Categories