imdong
2020-05-21 22:24:16 +08:00
```
#!/bin/bash
time_record_file="$0.time"
now_time=`date +%s%3N`
time_diff=${now_time}
if [ "$1" == 'start' ]; then
echo ${now_time} > ${time_record_file}
exit;
elif [ "$1" == 'start_run' ]; then
echo '0' > ${time_record_file}
fi
if [ -f $time_record_file ]; then
old_time=`cat ${time_record_file}`
time_diff=`expr ${now_time} - ${old_time}`
fi
if [ ${time_diff} -lt 259200 ]; then
exit
fi
echo ${now_time} > ${time_record_file}
############# 开始你的代码
echo "开始运行了"
```
=============
如你帖子所希望的,每次运行前检查距离上次运行
至于 cron 要定义 每分钟执行一次,还是每天执行一次,自己取舍吧。
cron 里面 /path/script 定时运行即可
开机启动加入 /path/script start (但开机时不会执行代码)
如果需要开启时也执行一次代码,就开机启动加入 /path/script start_run
参考资料:Google Search
Google 关键词:
1. shell 时间戳
2. shell 文件是否存在
3. shell 减法
4. shell if 小于