Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple PHP scripts run at same time in Cron

Tags:

php

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.

like image 943
Daniel Jørgensen Avatar asked Dec 14 '22 05:12

Daniel Jørgensen


2 Answers

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
like image 109
szenbalu Avatar answered Dec 25 '22 11:12

szenbalu


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.

like image 30
Barmar Avatar answered Dec 25 '22 09:12

Barmar