4

I have a crontab that looks like the following

* 22 * * * php /var/www/domain1/cron.php
* 22 * * * php /var/www/domain2/cron.php
* 22 * * * php /var/www/domain3/cron.php
* 22 * * * php /var/www/domain4/cron.php
...

However the scrips shown above seems to mess up and run op to a hundred times each! Im not sure why this happens, but since they are all set to start at the same time i would try and change this. It should be said that if i run each cron file manually i see no such behavior and get the expected behavior.

Can i somehow let cron only run one line at a time ? So that when domain1/cron.php is run it will not run domain2/cron.php before domain1/cron.php has finished?

I cannot change the time for them, since i cannot be sure when each one finishes. On one domain it might take 3 seconds while on another it might take 30 minutes.

2
  • 1
    * 22 means to run the script at 22:00, 22:01, 22:02, 22:03 through 22:59. So each script will be run 60 times. Commented Jul 12, 2016 at 5:41
  • Turns out that this is my error. Stupid me. Ofcourse it should only run once, which means it should be 0 22 * * * Commented Jul 12, 2016 at 7:38

3 Answers 3

7

If you have the root permission you can try this way.

* 22 * * * /root/sbin/allDomainsCron.sh

Where allDomainsCron.sh contains your domains cron.php one by one:

#!bin/bash
php /var/www/domain1/cron.php
php /var/www/domain2/cron.php
php /var/www/domain3/cron.php
php /var/www/domain4/cron.php
...

So you can ensure that only one cron.php run at a time.

If you would run the script once a day at 22:00 this code will do the work:

0 22 * * * /root/sbin/allDomainsCron.sh
Sign up to request clarification or add additional context in comments.

3 Comments

I've chosen this approach along with fixing my error being that I've not intended to run the script every minute between 22 and 23, but only once.
Why does he need root permission? The script doesn't have to be in /root, it can be in his own directory.
You're right, he don't need to have root permission if the script is elsewhere. He need to have root permission when use my example above, when the script is under the /root directory.
5

Put them all on a single command line, so they will be run sequentially.

* 22 * * * php /var/www/domain1/cron.php; php /var/www/domain2/cron.php; php /var/www/domain3/cron.php; php /var/www/domain4/cron.php

But since this cron job runs every minute during hour 22, you'll still get overlap if they take more than a minute.

Comments

0

No, a crontab does not offer you in-order execution. It's also unreliable to use cron this way. What you're probably wanting instead is an actual queue or job manager that can effectively control the outcome and flow of your desired job execution much more reliably than crontabs can. You could checkout something like RabbitMQ, ZeroMQ, or Gearman for this. Or if you're using AWS, SQS is a good start.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.