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

时间:2016-02-10 19:17:56

标签: linux bash sh

我可以访问Linux CentOS盒子。 (我不能遗憾地使用crontab)

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

#!/bin/sh


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

sleep 1438m
done

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

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

2 个答案:

答案 0 :(得分:2)

检查循环中的时间,如果不是你想要的时间,请睡一会儿。

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

答案 1 :(得分:2)

你有(至少!)三种选择:

  1. cron

    这是最好的选择。不幸的是,你说它不适合你。拖动:(

  2. at

      

    at和批量读取标准输入或指定文件的命令   这些将在以后执行。

    例如:at -f myjob noon

    以下是有关athttp://www.thegeekstuff.com/2010/06/at-atq-atrm-batch-command-examples/

  3. 的更多信息
  4. 写一个"轮询"或者" while loop"脚本。例如:

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

    以下是"计算等待时间":Bash: Sleep until a specific time/date

  5. 的一些好主意