Cron Job with PHP and an increment variable argument - php

I would like to know of it is possible to execute a Cron job every 30 seconds and increment an argument from a value (1 for exemple) to another (50000) :
Like :
wget https://mon-url.com/file/cron.php?id=1 >/dev/null 2>&1
wget https://mon-url.com/file/cron.php?id=2 >/dev/null 2>&1
wget https://mon-url.com/file/cron.php?id=3 >/dev/null 2>&1
wget https://mon-url.com/file/cron.php?id=4 >/dev/null 2>&1
....
wget https://mon-url.com/file/cron.php?id=50000 >/dev/null 2>&1
Is there any command to do that programaticaly ?
Thanks

As suggested before i'd rather go with bash script like this (or similar):
#!/bin/bash
i=1;
while [ $i -le 5 ]
do
wget https://mon-url.com/file/cron.php?id=$i >/dev/null 2>&1
i=$(($i+1));
sleep 30
done
Regards.
Ps. change 5 after -le to whathever you need

Look here below you want:
$x = 1; // You value set to minim
$y = 50000; // Your value set to maxim
while($x <= $y) {
echo "wget https://mon-url.com/file/cron.php?id=$x >/dev/null 2>&1";
$x++;
}
You can use this in your script for cron jobs. Good look!

Perhaps u can just export your counter incrementing it by one every time
COUNTER=0
*/30 * * * * wget https://mon-url.com/file/cron.php?id="$COUNTER" >/dev/null 2>&1 && export COUNTER=$((COUNTER+1))

Related

PHP how to run multi bash command in background

To run one command in the background, it works well.
$cmd = 'ffmpeg -re -i ./97.mp4 -vcodec copy -acodec copy -f flv -y rtmp://example.com/c/190843?auth_key=7e2682b5 > output 2>&1 </dev/null &';
exec($cmd, $output, $return_var);
Then I need to sleep for some time before the ffmpeg command. I refer to How do I run multiple background commands in bash in a single line? which works well directly in the bash console.
While not works in the below PHP script, which will return when the bash command finishes running.
$cmd = '(sleep 5; ffmpeg -re -i ./97.mp4 -vcodec copy -acodec copy -f flv -y rtmp://example.com/c/190843?auth_key=7e2682b5 > output 2>&1 </dev/null) &';
exec($cmd, $output, $return_var);
I think you also need to handle sleep's stdout/stderr.
( sleep 5 > /dev/null 2>&1; ...; ) &
Or you can put the redirection after ( ... ):
( sleep 5; ffmpeg ...no redir here...; ) < /dev/null > /dev/null 2>&1 &
To run multiple commands from on bash command; concatenate using &&.
For instance:
sleep 5 && echo hello && sleep 2 && echo world
This trick can be particularly useful in cron tasks to be able to sequence multiple items within the same minute, as a different sleep value can be used as an offset before starting the command.

One command - two cron job

I need to execute in one command two php files.
the second file need to run right after the first finish.
This is what i did, not sure if it's ok:
/usr/bin/wget -O /dev/null -o /dev/null https://example.com/scripts/cron.php; sleep 2; wget -q -O - https://example.com/cron2.php > /dev/null 2>&1
I added sleep between the commands, it will work?
You can use && for sequential execution of command,
check https://www.steveroot.co.uk/2010/07/05/cron-multiple/ And What is the purpose of "&&" in a shell command?
In Your case You can try :
01 00 * * * //usr/bin/wget -O /dev/null -o /dev/null https://example.com/scripts/cron.php && wget -q -O - https://example.com/cron2.php > /dev/null 2>&1
Hope these will Help.

Cron Job Scheduled did not run on specified time

I scheduled a cron job to hit a page 1st of every month at 12.00AM but the cron didn't work for some reason.
The below is the cron I have used :
0 0 1 * * /usr/bin/php /var/www/html/cronleave.php >/dev/null 2>&1
Any help will be appreciated.
using sample :
0 0 1 * * wget -O /dev/null -o /dev/null http://www.domain.com/cronleave.php >/dev/null 2>&1
and check time server

Using wget with shell_exec and at command?

I've been struggling with shell_exec PHP function and at linux command for 2 days.
To make it short, this works:
shell_exec('/usr/bin/at 09:32 <<EOF
touch /var/www/website/hello.txt
EOF'
);
this doesn't:
shell_exec('/usr/bin/at 09:32 <<EOF
wget -O - -q -t 1 "http://192.168.56.101/website/test.php?param=hello" >/dev/null 2>&1
EOF'
);
Why ?
(note: the code above does work in console)
Thanks in advance.
Ok I've got it at last !!
For those who are interested the pb comes that the wget command also need to be invoked with the full path (ie: /usr/bin/wget).
What misleaded me is that the touch command doesn't need it. It's weird but anyway here's the working code:
shell_exec('/usr/bin/at 09:32 <<EOF
/usr/bin/wget -O - -q -t 1 "http://192.168.56.101/website/test.php?param=hello" >/dev/null 2>&1
EOF'
);

PHP: exec() doesn't run in the background even with ">/dev/null 2>&1 &"

I'm calling this in my php script:
exec("gutschein.php >/dev/null 2>&1 &");
Calling the script (generates a pdf and sends it away by e-mail) works, but the process is not running in the background (I checked it out with a sleep statement inside gutschein.php). The browser is hanging until execution of gutschein.php is finished.
I also checked out the following:
exec("/usr/bin/php gutschein.php >/dev/null 2>&1 &");
or
shell_exec("/usr/bin/php gutschein.php >/dev/null 2>&1 &");
It doesn't change anything. The script is actually running on a linux server. Has anybody an idea what I'm doing wrong?
Try system and/or passthru. I've had issues with exec before because it halts trying to fill the return array with data until the process has finished.
These will both echo raw output so even if they work you may need to handle that with a discarded output buffer.
/**
* #author Micheal Mouner
* #param String $commandJob
* #return Integer $pid
*/
public function PsExec($commandJob)
{
$command = $commandJob . ' > /dev/null 2>&1 & echo $!';
exec($command, $op);
$pid = (int) $op[0];
if ($pid != "")
return $pid;
return false;
}
This worked for me .. check it
also, return processId of the background process
All output must be redirected, else the script will hang as long as gutschein.php executes. Try
exec('/usr/bin/php gutschein.php &> /dev/null &');
Can you try one of the following 2 commands to run background jobs from PHP:
$out = shell_exec('nohup /usr/bin/php /path/to/gutschein.php >/dev/null 2>&1 &');
OR
$pid = pclose(popen('/usr/bin/php gutschein.php', 'r'));
It will execute the command in background and returns you the PID, which you can check using condition $pid > 0 to ensure it has worked.
You can use screen: screen -d -m /usr/bin/php gutschein.php
Here is screen's manual if you need more info on options.
Please see my experience at HERE.
The way I try and works for me -
php -q /path/to/my/test/script/cli_test.php argument1 < /dev/null &
In PHP, it is like
exec('php -q /path/to/my/test/script/cli_test.php argument1 < /dev/null &')
The browser is might waiting for a response, so let your script produce any output and terminate the "main process". a simple
die('ok');
should do the job.
btw, forking a new process by calling exec is might not the best solution since the new process isn't a real child process - means you can't control it. you might consider using pcntl (http://php.net/manual/de/book.pcntl.php) for this purpose. but stand clear, this extension has also his pitfalls, especially in the context of a webserver.

Categories