My cron file are stopping after some time and restarting automatically - php

I am running a php file via cron job every 5 hrs as it takes long time (Approx 4 hrs sometime). But I am noticing that some way its restarting again and again during this 5 hrs gap. (I can check it via my log file.)
Its getting some Connection time out on run manually via wget. May be this is the reason the cron stopping and running several time.
Can any one suggest me any idea to overcome this situation.

Related

PHP cron job per minute and real time

i need to build a web application that will keep update the mysql database every minute and the data will be real time on browser.
The things i only care about is the cron job.
I was thinking if early stage the cron job need less than a minute to finish, but at late stage it might need more than a minute to complete the process. May i know that Will it crashed the process ?
Like Process A is still running, but later a minute Process B already start to run and Process A is still running. (Will it cause database data corrupted problem?)
Thanks.

Cron is not running at specified time in moodle?

Cron is not running at specified time in moodle?
1.I just created an cron job like a
function cron(){} in block_plugin and i written an code to insert records in db and i set time $plugin->cron= 1*60; in version.php. its inserting data into db when i am running mysite.com/admin/cron.php.
2.Its not working for every specific time(automatically for every 1 min).I checked db after 1min without running yoursite.com/admin/cron.php file its ideal. please help in it.
If you are not manually running admin/cron.php, then have you set up something to automatically call this on a schedule? (Or admin/cli/cron.php)?
If you have not set up an automatic process to run the Moodle cron and have not called it manually, then none of the cron processes will happen.
Note that specifying 60 seconds is the minimum time between your code running - it will depend on how long the other cron processes take (I've seen large sites where cron can take 30-40 min to run at times) and how often the automated process calls it (could be every 1 min, but many sites set it to every 5-10min, or longer, to reduce server load).

every minute cron job sometimes not running

I have several cron jobs executing php scripts. The php scripts sometimes do some heavy jobs like updating hundreds of records at a time in a mysql table.
The problem is that the job should run every minute. However, it randomly misses and as a result does not execute every minute. Sometimes, it executes every 4-6 minutes, then back to every 1 minute, misses 2-3 times more and then normal again.
I am on centos 6.5
Please note that the php runs correctly and there is no problem whatsoever concerning the php scripts themselves since on the time it runs, I get the expected results and that there are about 10 more other similar scripts running at the same time (every minute or every 5 minutes for the other scripts)
Job:
/usr/bin/php "/var/www/html/phpScriptToExecute.php" >> /var/www/html/log/phpScriptLog.log 2>&1
My take is that it is maybe a problem with too many simultaneous scripts running concurrently, accessing the database at the same time.
Last information: No error in the /var/log/cron file or in the phpScriptLog.log file.
The reason could be, your cron job takes more than 1 minute to execute, print out the start time and end time at the end of the script to validate it.
if the cron job is running, linux won't execute it again.
My guess is it's caused b a PHP fatal error, but your PHP probably isn't configured to send error messages to stderr, which is why your phpScriptLog.log is empty. You could check your php.ini (or just use ini_set()) for the following:
display_errors: set it to on/true if you want errors to show on stderr
log_errors: set it to true if you want to send the error messages to a file
error_log: point it to a file you want the errors to be stored in
Or, if you want a solution to avoid overlapping cron jobs, there are plenty here in SO.

Use PHP and Windows Task Scheduler to try again on exit(1)

I use windows task scheduler to start up a php script that works perfectly fine. Basically C:\php.exe -f C:\myscript.php
In my script some work happens that sometimes makes me want to run the task script again in 5 minutes.
I tried to implement this by changing the settings of the task to restart every 5 minutes if the task fails and having my php code exit(1). The task scheduler seems to know that I exited with an error code of 1, but it does not run the script again.
Does anyone know what I can do to make it so that task manager will try again in 5 minutes if I signal it from my code somehow.
Not an answer to the question as phrased, but might serve as a fallback if you can't get it working: make your job run every 5 minutes, regardless, and then track "last success"/"last failure" yourself, in a database or file.
Before doing anything else, the script can check the logged status, and if there was a failure last time, try again (up to a limited number of tries, presumably). If there was a success last time, exit immediately, unless it's time for the next job anyway (e.g. if the original schedule was daily, then check for $last_success being longer ago than 24 hours).

PHP with lengthy loop creating erratic server error

I have a PHP script that pulls down a bunch of RSS feeds. To prevent overloading the publishers' servers, I use the PHP Sleep function to slow things down.
The entire script could last for a couple of hours.
If I run this from a Cron job on GoDaddy, it will happily work for 5 - 10 minutes and then return a server error. I checked and the PHP maximum execution time is 30 seconds, so I'm not sure if this is the cause of the problem.
If I run the job on my Mac, my local PHP also has a default maximum execution time of 30 seconds, but this script does work if I run it from the terminal, but I don't understand why.
How do I loop a script that will exceed 30 seconds without running into unreliability problems?
Help appreciated.
Short answer is use set_time_limit(0) to allow for a long-running script. Your terminal (CLI) PHP probably has it set to 0. You could also be running out of memory, especially on PHP 5.2 or older. Log all errors to a file, and inspect it.
You could rewrite your program to be able to work on a subset of the data during one run. The benefit of that approach is you could use it to run 24/7 or to run every five minutes, depending on what the PHP environment supports. You could also run multiple instances at a time, each working on their own data.

Categories