Activate Cron job from a PHP page - php

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.

Related

PHP script with crontab loop

I have a php script that have a long execution time.
I've set
set_time_limit(0);
ini_set('max_execution_time', 36000);
and with the Browser the script goes well.
But when I call with crontab like
0 3 * * * wget -q -O - "http://mysite.it/script.php"
I can see that the script start and finish (I've a logger implemented) but the log tell me that the script is continously invoked as if crontab does not recognize the end of the script and it did start again every time.

How to run cron job in php

I have php function that I wnat to run every 15 minutes. I found some questions like this:
How can I make a cron to execute a php script?
and in my case I should use this:
*/15 * * * * /usr/local/bin/php -q /path/to/my/file.p
But should I run this command in terminal or put it in my file? And once, it is executed, will it run all the time or will have time limit?
Thanks!
PHP doesn't run cron jobs, your server (or operating system) is doing this. There are two ways to put your cron job to work:
#1
Using the shell command crontab. The command crontab -l will list all existing cronjobs for your user (most likely there are none yet). crontab -e will open an editor window where you can put in a you cron job as a new line. Save, and your cron job is now running. crontab -l again and you will see it listet. crontab -r to remove all the cont jobs.
You can also start a cron job from a file. Simply type crontab filename (eg. crontab textfile.txt)
Alternatively you can also start it from within PHP. Just put your cron job into a file and start it via exec() like so:
file_put_contents( 'textfile.txt', '*/15 * * * * /usr/local/bin/php -q /path/to/my/file.php' );
exec( 'crontab textfile.txt' );
#2
If you have admin privileged on your system you can create a file in /etc/cron.d/ (for example, call it my_cronjob) and put your cron job there. In this case you probably want to run it as a user (not as admin, that would be rather insecure). This is quite easy to do, just add the user name like so:
*/15 * * * * user_name /usr/local/bin/php -q /path/to/my/file.p
(In this case the cron job will not be listet under crontab -l)
Answering your second question: As long as the cron job is listet in crontab -l or as long as the file is sitting in /etc/cron.d the cron job is running, in your case, every 15 minutes.
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script. More on this later.
Please read this tutorial http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

Timeout issue when running background PHP script as web

I've got a PHP script that does some heavy lifting that I'm trying to fire off as a background script using the following code:
exec("script.php > /dev/null 2> /dev/null &");
What happens:
When I run the code above as part of my web app, the script quits
after about a minute.
When I run the code as part of my web app without the final
ampersand, the job runs fine - but exec() waits for the script to
complete before loading the next page, defeating the purpose as the user stares at an unresponsive page.
When I run the shell command script.php > /dev/null 2> /dev/null & as myself from the console with the final ampersand, the job runs fine.
When I run the shell command from the console as web, the job stops running after about a minute.
I've tried piping my output to logfiles: script.php > /home/public/tmp/output.txt 2> /home/public/tmp/errors.txt &. Output looks normal, I don't get any errors. The script just stops working.
The basic rule seems to be: If run as a foreground process as web or as me, it'll complete. If run as a background process as web, it stops working after about a minute.
I'm not running this as a cronjob because my host (NearlyFreeSpeech) can only run cronjobs once an hour, which is more than I want to make users wait for when the job only takes a couple minutes- it might as well fire when users initiate it.
The subscript starts with set_time_limit(60 * 60 * 4); so this shouldn't be a matter of PHP timing out.
set_time_limit
does not include shell-execution time.
http://php.net/manual/en/function.set-time-limit.php
Try using of the code examples in the comments on that site.

Running cron job from browser

I have several cron jobs that run automatically, and I was requested to add a button that says 'run now' in the browser... Is this possible? Some of these cron jobs need to be executed from command line as they take around 15 minutes... Is it possible to execute them from the browser, not as a normal php function but somehow trigger an external php from the browser?
You're looking for the exec() function.
If it's a 15 minute task, you have to redirect its output and execute in in the background. Normally, exec() waits for the command to finish.
Example: exec("somecommand > /dev/null 2>/dev/null &");

Cron Job PHP script execution time report

My question is simple: I want to know how long a PHP script is taking to execute. On top of this, I am executing it via cron. Now, I could do something via the PHP code itself to get the execution time start/end, however I wondered if there was something via the cron command that I could add to get that emailed to me, in milliseconds?
Currently I am using:
/usr/bin/php -q httpsdocs/folder/script.php > /dev/null 2>&1
Which runs my script and stops all errors/output getting emailed to me. Can I change the above to get the execution time emailed to me somehow?
Thanks
/usr/bin/time /usr/bin/php -q httpsdocs/folder/script.php
| mail -s "Some Subject" you#youremailid.com
:-)
You can use time command like this:
/usr/bin/time /usr/bin/php -q httpsdocs/folder/script.php > /var/log/crontiming
By the given script you can change the cron job execution time.
$aCronList = array();
$result = -1;
exec('crontab -l', $aCronList, $result);
foreach($aCronList AS $item)
{
// Find what you want, replace the times
}
$sCronList = implode(PHP_EOL, $aCronList);
exec('crontab < ' . $sCronList);

Categories