woods1815

 

目前在crontab中最小执行时间单位为分钟。

如果需要按秒来执行,有以下两种方法:

 

方法一:通过sleep来实现

例:

1、创建test.php文件,这里测试通过打印时间好区分。

<?php
    file_put_contents(\'log.txt\',date(\'Y-m-d H:i:s\') . "\n", FILE_APPEND);
?>

2、确保单独访问test.php文件能打印日志。

3、编辑crontab文件,通过crontab -e 命令,比如我要每15秒运行一次,内容如下:

* * * * * curl "http://127.0.0.1/testtask/test.php" && sleep 15;curl "http://127.0.0.1/testtask/test.php" && sleep 15;curl "http://127.0.0.1/testtask/test.php" && sleep 15;curl "http://127.0.0.1/testtask/test.php"

4、打印结果,可以通过 tail -f log.txt 命令实时查看结果。

可以看到每15秒打印出来结果。

 

 

方法二:通过添加中间shell脚本来实现

例:

1、添加脚本文件 test.sh,内容如下:我这里是选择2秒执行一次。

step=2 #间隔秒数
for ((i = 0; i < 60; i = (i + step))); do
    $(curl "http://127.0.0.1/testtask/test.php")
    sleep $step
done
exit 0

2、编辑crontab文件

* * * * * /phpstudy/www/testtask/test.sh

3、打印结果

 

分类:

技术点:

相关文章:

  • 2021-11-19
  • 2021-12-10
  • 2018-05-15
  • 2021-11-19
  • 2021-07-10
  • 2021-04-06
  • 2021-11-12
猜你喜欢
  • 2021-07-06
  • 2021-11-12
  • 2021-06-16
  • 2021-09-29
  • 2021-09-23
  • 2021-11-29
  • 2021-11-19
相关资源
相似解决方案