我设置了一个 cron 作业,但由于某种原因,它在晚上无法运行。当我在白天运行它时,同一个作业运行良好(即设置同一个作业在几分钟内运行并等待它运行)。
一些日志:
Jun 17 14:04:01 server CRON[20174]: (root) CMD (/home/server/scripts/sync.sh >> /home/server/logs/nightly.log 2>&1)
Jun 17 14:12:01 server CRON[20349]: (root) CMD (/home/server/scripts/sync.sh >> /home/server/logs/nightly.log 2>&1)
Jun 17 14:23:01 server CRON[20513]: (root) CMD (/home/server/scripts/sync.sh >> /home/server/logs/nightly.log 2>&1)
Jun 18 01:00:01 server CRON[25717]: (root) CMD (/home/server/scripts/sync.sh >> /home/server/logs/nightly.log 2>&1) # this runs during the night
Jun 18 01:00:01 server CRON[25706]: (CRON) error (grandchild #25717 failed with exit status 2) # doesn't leave anything behind it, not even the nightly.log file - as though the command itself is invalid)
cron 条目如下所示:
0 1 * * * /home/server/scripts/sync.sh >> /home/server/logs/nightly.log 2>&1
脚本如下所示:
#!/bin/bash
SUCCESS=0
echo "Running nightly automation..."
echo "Running xxxx script..."
SUCCESS=`/home/server/scripts/xxxx.sh && echo 1`
if [ $SUCCESS -eq 1 ]; then
echo "xxxx success!"
else
echo "xxxx fail!"
fi
echo "Running rsync..."
/usr/bin/rsync -am --remove-source-files /path1/ /path2/ --log-file=/home/server/logs/sync.log && echo "rsync success!"
答案1
我已找到问题的根源。
事实证明,我启用了主目录加密,并且我的脚本驻留在该加密目录中。这就是为什么我自己运行脚本时从未失败的原因 - 我已登录,因此路径有效。希望这可以帮助其他人。