简体   繁体   中英

Loop in php script from cron?

I have a wordpress php script running as a cron job. it's currently running once a minute per domain indefinitely.

    /usr/bin/php -q /home/domain1/public_html/wp-content/plugins/tdw/postit.php > /dev/null 2>&1

The problem is my server is getting big with 50+ domains and the crons are running into each other causing errors or crashing the database.

is there a way that i can make it run sequentially? like run domain1 script wait one minute then run domain2...looping through the url list.

thank you for your help.

Put them in a bash script, like

#!/bin/bash

/usr/bin/php -q /home/domain1/public_html/wp-content/plugins/tdw/postit.php > /dev/null
sleep 60s
/usr/bin/php -q /home/domain2/public_html/wp-content/plugins/tdw/postit.php > /dev/null
sleep 60s
/usr/bin/php -q /home/domain3/public_html/wp-content/plugins/tdw/postit.php > /dev/null
sleep 60s
# and so on...

and start that from cron.

If the pathes are all alike except the domain name, you could also use a php script, which generates the correct path for every domain and then executes the script via exec() . For example, it could iterate over the contents of the /home folder via the directory functions of PHP. That way, you do not have to update your script when you add a domain.

A script like that would look like

<?php

$template = '/usr/bin/php -q /home/%s/public_html/wp-content/plugins/tdw/postit.php > /dev/null';

if ($handle = opendir('/home')) {
    while (false !== ($entry = readdir($handle))) {
        $command = sprintf($template, entry);
        exec($command);
        sleep(60);
    }
}

?>

Just got a similar issue on a script I'm executing on my NAS via cron, turned out that there was an error in my query, I did miss the quotes

WHERE myvarcharfield = 12345

istead

WHERE myvarcharfield = '12345'

not sure why it seemed that script executed correctly anyway and after that break the loop but it also did not return an error

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