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
- 通过多个crontab实现
https://blog.csdn.net/shootyou/article/details/6918538
我们都知道crontab的粒度最小是到分钟,但是我们还是可以通过变通的方法做到隔多少秒运行一次。
以下方法将每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
网友评论