【问题标题】:How to run scripts depend on difference stop time如何运行脚本取决于不同的停止时间
【发布时间】:2019-09-10 22:49:55
【问题描述】:

您好,我编写了一个 bash 脚本,它每 24 小时自动运行我的 python 程序并更新参数。第 5 天后,它将使用最后坐下的参数运行。

我想添加选项,如果系统关闭超过 6 小时,脚本将从第一天选项开始。如果不是,它将从最后一个状态继续。

目前我只是将开始时间写入文件。但是我不确定如何从该文件中读取时间字符串并用于计算开始和停止之间的时间差。

我只是尝试将日期写入文件。但是我不太确定如何在异常中读取和使用这个值。

echo "$(date) $(ls -1 | wc -l)" >> starttime.txt
python3 startmeVtest.py 5 2 10
timeout 86400 ./start.sh
sleep 4
python3 startmeVtest.py 10 4 20
timeout 86400 ./start.sh
sleep 4
python3 startmeVtest.py 20 4 40
timeout 86400 ./start.sh
sleep 4
python3 startmeVtest.py 30 8 50
timeout 86400 ./start.sh
sleep 4
python3 startmeVtest.py 50 9 70
./start.sh

如果系统已停止少于 6 小时,则应执行最后一个脚本

sleep 4
python3 startmeVtest.py 50 9 70
./start.sh

【问题讨论】:

  • After 5-th day it will run with last "seated" parameters 是什么意思?和However I'm not quite sure how to read and use this value in exception ?
  • 抱歉,我的英文可能不太清楚。坐下 -> 设置参数。我想在 If 操作数中使用时间和日期值

标签: linux bash date ubuntu time


【解决方案1】:

假设文件starttime.txt 的最后一行始终包含开始时间,首先您可以读取它并转换为秒数(自 1970-01-01 00:00:00 UTC 起,请参阅 man date)如下。

d1="$(date -d "$(cat starttime.txt | sed -nE '$ s/(.*) [0-9]+$/\1/p')" +%s)"

现在您可以计算nowd1 之间的时间差(以秒为单位)。

diff="$(( $(date +%s) - d1 ))"

你可以用这个值来做决定。

if [[ $diff -gt 21600 ]]; then
    # action if diff is more than 6 hours
else
    # action if diff is less than 6 hours
fi

也许您可以通过将重复的命令移动到一个函数中来简化您的脚本。所以最终的脚本将如下所示。

run_script()    # reusable functionality
{
    python3 startmeVtest.py $@
    timeout 86400 ./start.sh
    sleep 4
}

# read the last timestamp first
d1="$(date -d "$(cat starttime.txt | sed -nE '$ s/(.*) [0-9]+$/\1/p')" +%s)"
# then save the current timestamp
echo "$(date) $(ls -1 | wc -l)" >> starttime.txt

# check difference with current time
if [[ "$(( $(date +%s) - d1 ))" -gt 21600 ]]; then
    # action if diff is more than 6 hours
    run_script 50 9 70
else
    # action if diff is less than 6 hours
    run_script 5 2 10
    run_script 10 4 20
    run_script 20 4 40
    run_script 30 8 50
fi

# run following always, irrespective of the time diff
./start.sh

【讨论】:

  • 非常感谢您的回答!!接下来我尝试了:(出于测试目的,我使用了 10 秒) d1="$(date -d "$(cat starttime.txt | sed -nE '$ s/(.*) [0-9]+$/\ 1/p')" +%s)" echo "$(date) $(ls -1 | wc -l)" >> starttime.txt if [[ "$(( $(date +%s) - d1 ) )" -gt 10 ]]; then echo "less then 10 sec" #echo $d1 # action if diff is more than 6 hours else echo "more then 10 sec!!!" #echo $d1 fi 并且总是得到结果:日期:无效日期 'Sun 21 Apr 23:50:32 AEST 2019' 少于 10 秒,并且由于某种原因,在 starttime.txt 中最后有数字 12 Sun 21 Apr 23 :50:42 澳大利亚东部标准时间 2019 年 12 月
【解决方案2】:

考虑在您转储到 starttime.txt 的日期命令中使用不同(或附加)格式。

代替:

echo "$(date) $(ls -1 | wc -l)" >> starttime.txt

考虑:

echo "$(date +%s) $(ls -1 | wc -l)" >> starttime.txt

上述编辑将以纪元时间输出时间戳。纪元时间是自纪元以来的秒数(通常是格林威治标准时间 1970 年 1 月 1 日)。整数值会让你更容易比较 6 小时的时差:

这里有一个小代码来查看 starttime.txt 中的最后一个条目是否是六个小时前的:

lastrun=$(tail -1 starttime.txt | awk '{print $1}')
currenttime=$(date +%s)
if [ $currenttime -gt $(( lastrun + 6 * 60 * 60 )) ] ; then
  # execute the system has STOP more than 6 hours logic
  :
else
  # execute the system has STOP less than 6 hours log
  :
fi

但是,您也可以通过查看 starttime.txt 上的时间戳并将其与另一个 6 小时前的文件进行比较来简化此操作。这是使用 -ot 测试的另一种方法。 -ot 是比测试更早的。以下是如何将其应用于您的用例:

touch -d "6 hours ago" 6hoursago
if [ starttime.txt -ot 6hoursago ] ; then
  # execute the "system has STOP more than 6 hours logic
  :
else
  # execute the "system has STOP less than six hours
  :
fi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 2012-07-29
    相关资源
    最近更新 更多