我想在特定时间运行一个脚本而不使用 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