Citizen SP
Citizen SP

Reputation: 1411

Run 50 php scripts every 5 minutes

I have 50 Wordpress installations running on 1 server. Every Wordpress website has a script which must execute every 5 minutes. I don't want to use the build-in cron of Wordpress and want to run the scripts from cron. What is the best solution?

*Solution 1: create a cron job for every individual wordpress installation *

5   *   *   *   *   /usr/bin/php /wordpress-site-1/cron.php
5   *   *   *   *   /usr/bin/php /wordpress-site-2/cron.php
5   *   *   *   *   /usr/bin/php /wordpress-site-3/cron.php
... + 47 other scripts

*Solution 2: create 1 cron job and run a bash script *

 5  *   *   *   *   /run/the-script

The script:

 #!/bin/bash
    # include the 50 sites in array
    array=( wordpress-site-1 wordpress-site-2 wordpress-site-3 ...)
    for i in "${array[@]}"
    do
        {execute php file) 
    done

Upvotes: 1

Views: 1057

Answers (3)

oohcode
oohcode

Reputation: 429

if you want every script run per 5 min, you can do this:

*/5 * * * * /run/the-script

Upvotes: 0

MLSC
MLSC

Reputation: 5972

This is a pattern that you could use:

0,5,10,15,20,25,30,35,40,45,50,55 * * * *

try this solution:

cat cronjob
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/php /wordpress-site-1/cron.php
 .
 .
 .
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/php /wordpress-site-50/cron.php

Then:

chmod +x cronjob
chmod +x /wordpress-site-1/cron.php
 .
 .
 .
chmod +x /wordpress-site-50/cron.php

/etc/init.d/crond start  #redhat based servers like centos
/etc/init.d/cron  start  #debian based servers like ubuntu

crontab cronjob

NOTE: You can use for loop to make it easier.

Upvotes: 0

giorgio
giorgio

Reputation: 10202

By far, having 50 singular crons is much more efficient than have it run in one script. The only downside is that you'll have to insert 50 lines in your crontab. But hey, if that's everything...

Reasons for having 50 crons instead of one:

  • If one of the crons fails, the others will still run
  • If one of the crons fails, you'll be notified and don't have to do complex log-analysis to find out which one of your crons failed
  • You don't have to worry (or worry less) about script timeouts, memory usage, etc: 50 scripts executed in one time is about 50 times heavier then having them executed one by one
  • You can divide payload on the server
  • (You can probably think of some more)

As for the last point (dividing payload), I'd suggest to execute the first 10 scripts at zero, 5-past, 10-past etc, and the next 10 scripts a minute later. This way they don't run all at once, causing a high peak-load every 5 minutes:

0,5,10,15,20,25,30,35,40,45,50,55   *   *   *   *   /usr/bin/php /wordpress-site-1/cron.php
... cron 2 - 10
1,6,11,16,21,26,31,36,41,46,51,56 /usr/bin/php /wordpress-site-11/cron.php
... cron 11-20
... etc.

Upvotes: 2

Related Questions