![我应该怎么做才能在没有 cron 的情况下在特定时间运行脚本?](https://linux22.com/image/151671/%E6%88%91%E5%BA%94%E8%AF%A5%E6%80%8E%E4%B9%88%E5%81%9A%E6%89%8D%E8%83%BD%E5%9C%A8%E6%B2%A1%E6%9C%89%20cron%20%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%E5%9C%A8%E7%89%B9%E5%AE%9A%E6%97%B6%E9%97%B4%E8%BF%90%E8%A1%8C%E8%84%9A%E6%9C%AC%EF%BC%9F.png)
我想在特定时间运行一个脚本而不使用 cron。但它不起作用。
#!/bin/sh
DATE=`date | cut -d' ' -f4`
#Date is getting printed, if I run it manually, without any error. But file is not created at scheduled time.
echo $DATE
if [[ $DATE == "07:06:55" ]]
then
echo "this is a test program" >> xyz.log
fi
答案1
您可以使用at
:
at -f "$script" 'now + 24 hours' &>/dev/null
我还发现了这个:
watch -n <the time> <your command or program or executable>
答案2
如果您只想运行一次,可以使用 at 命令:https://en.wikipedia.org/wiki/At_(命令)
例子 :
echo "echo \"this is a test program\" >> /tmp/xyz.log" | at 1127 apr 11
如果你想每天运行它,你可能需要使用循环:
#!/bin/bash
while true;
do
DATE=`date | cut -d' ' -f4`
echo $DATE
if [[ $DATE == "11:33:00" ]]
then
echo "this is a test program" >> xyz.log
sleep 1s
fi
done