美文网首页PHP
2018-10-15 实现秒级定时任务

2018-10-15 实现秒级定时任务

作者: 爱吃秋葵的莫冲 | 来源:发表于2018-10-29 16:00 被阅读14次

    laravel的定时任务默认是使用crontab,最快只能1分钟执行一次。有以下几种方式可以实现秒级定时任务

    * * * * * /home/mao/Documents/demo.sh
    
    #!/bin/bash
    int=1;
    while(( $int<=60 ));
    do
            /usr/local/php/bin/php  /home/mao/Documents/crontab.php;
            let "int++";
            sleep 1;
    done
    

    以下方法将每20秒执行一次

    crontab -e 
    * * * * * /bin/date
    * * * * * sleep 20; /bin/date 
    * * * * * sleep 40; /bin/date 
    

    说明:需要将/bin/date更换成你的命令即可

    • 通过supersivor实现后台sh循环执行
      当然是写一个后台运行的脚本一直循环,然后每次循环sleep一段时间。
    #!/bin/bash
    filepath=$(cd "$(dirname "$0")"; pwd)
    cd $filepath
    while [ true ]
    do
      logname="translate_"$(date +%Y-%m-%d)".log";
      su www-data -c "php artisan article:translate 1>>./storage/logs/$logname"
        sleep 20
    done
    

    相关文章

      网友评论

        本文标题:2018-10-15 实现秒级定时任务

        本文链接:https://www.haomeiwen.com/subject/slqfzftx.html