dongyuduan1890 2016-03-17 17:00
浏览 43
已采纳

从cron循环PHP脚本?

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.

  • 写回答

1条回答 默认 最新

  • duanliusong6395 2016-03-17 17:09
    关注

    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);
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求会6sv辐射传输模型,辅导(可py6s🙏🏻有偿
  • ¥15 .xla后缀的文件拖到excel里什么内容也没有怎么办
  • ¥20 Workbench中Mechanical打不开、闪退是什么原因?
  • ¥240 MapReduce应用实践 学生课程
  • ¥15 hlss视频显示AUTHORITY_INVALID
  • ¥15 MAX9296A+MAX96717,美信gmsl解串有人做过吗?
  • ¥15 求帮我解决一下inode 爆满的问题(有偿)
  • ¥15 关于#vscode#的问题:布料滤波算法中C++实现pcl在Vscode中pcl库没有#include <pcl>
  • ¥15 fpga:ov5640采集tft显示
  • ¥100 python怎么连接wxSQLite3加密的数据库