Shell 无限循环在特定时间执行

Shell infinite loop to execute at specific time

我可以使用 Linux CentOS 盒子。 (可惜不能用crontab)

当我每天需要 运行 一项任务时,我刚刚创建了一个无限循环和休眠。 (它 运行 秒,睡了 ~24 小时,然后又 运行 秒)

#!/bin/sh


while :
do
    /home/sas_api_emailer.sh |& tee first_sas_api

sleep 1438m
done

最近我有一项任务需要在每天 6:00 的特定时间 运行 上午(我不能使用 crontab)

如何创建一个只执行 @6:00 am 的无限循环?

检查循环中的时间,如果不是你想要的时间就睡一分钟。

while :
do
    if [ $(date '+%H%M') = '0600' ]
    then /home/sas_api_emailer.sh |& tee first_sas_api
    fi
    sleep 60
done

您有(至少!)三个选择:

  1. cron

    这无疑是最佳选择。不幸的是,你说这不是你的选择。拖动:(

  2. at

    at and batch read commands from standard input or a specified file which are to be executed at a later time.

    例如:at -f myjob noon

    这里是关于 at 的更多信息:http://www.thegeekstuff.com/2010/06/at-atq-atrm-batch-command-examples/

  3. 写一个"polling"或"while loop"脚本。例如:

    while true
      # Compute wait time
      sleep wait_time
      # do something
    done
    

    以下是 "compute wait time" 的一些好主意:Bash: Sleep until a specific time/date