zoukankan      html  css  js  c++  java
  • Linux 定时任务 Crontab按秒执行

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

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

    方法一:通过sleep来实现

    例:

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

    <?php
        file_put_contents('log.txt',date('Y-m-d H:i:s') . "
    ", 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、打印结果

  • 相关阅读:
    洛谷-P1427 小鱼的数字游戏
    洛谷-P1047 校门外的树
    洛谷-P1046 陶陶摘苹果
    洛谷-P1980 计数问题
    洛谷-P1424 小鱼的航程(改进版)
    洛谷-P1423 小玉在游泳
    洛谷-P1035 级数求和
    洛谷-P1008 三连击
    Oracle 11g r2 rac +openfiler 2.99 安装
    26 主备库并行复制策略
  • 原文地址:https://www.cnblogs.com/woods1815/p/10959223.html
Copyright © 2011-2022 走看看