2

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.

user1766169
  • 1,932
  • 3
  • 22
  • 44
  • Maybe something I need to investigate is that whether there is a limit on the number of concurrent php scripts that can run. As I said earlier, at times there are multiple php scripts that can run at the same time because of multiple cron jobs executing php scripts. – Suyash Sumaroo Feb 07 '15 at 06:17
  • One more thing - I am using the framework codeigniter for my project. Maybe the problem lies in there - I will try to create some flat php files for the same functionalities and check if the flat php files run correctly compared to the scripts launched using codeigniter. – Suyash Sumaroo Feb 07 '15 at 06:26

2 Answers2

1

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.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • Thanks for you answer. I already added logs to check that. It would normally take more than 1 minute when there are certain things to process which is normal and something I expect. However, even when there is nothing to process by the script, it should normally take 1-2 seconds to complete - this problem happens even at these times. – Suyash Sumaroo Feb 05 '15 at 10:29
  • @SuyashSumaroo so that reason was basically it took longer than 1 min right? – Danyal Sandeelo Feb 06 '15 at 05:31
  • Yes and no. Let me explain. It's normal that sometimes the script takes longer than 1 minute and the cron job does not execute. However most of the time, the script should not take more than 5 seconds. Even at these times, sometimes the cron job does not run every minute. – Suyash Sumaroo Feb 07 '15 at 06:15
0

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:

  1. display_errors: set it to on/true if you want errors to show on stderr
  2. log_errors: set it to true if you want to send the error messages to a file
  3. 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.

aljo f
  • 2,430
  • 20
  • 22
  • Thanks for you answer. However, I added the following line in the php file: ini_set('display_errors', 1); - so the errors should normally be added to stderr as you mentioned. As for the overlapping problem, I also added log entries to check whether the script is being executed but I do not even get the log entries - I think that you are maybe right, that there is a php error somewhere - maybe timeout or something like that. I will check in that direction. – Suyash Sumaroo Feb 05 '15 at 10:21