简体   繁体   中英

Multiple PHP scripts run at same time in Cron

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM