我正在尝试让一个脚本创建一个 cron 作业,该作业在重启时执行另一个脚本。由于我尚不清楚的原因,我无法让它工作。
为了简单起见,这是我无法运行的片段。
创建 cron 作业的脚本:
#!/bin/bash
crontab /home/user/folder/tmp/crontab-reboot-part-2.txt
/crontab-reboot-part-2 的内容:
@reboot /home/user/folder/script.sh >> /home/user/folder/cron.log 2>&1
即使在重启之前手动执行 crontab 命令,
crontab /home/user/folder/tmp/crontab-reboot-part-2.txt
重新启动后,由于尚未创建 cron.log,因此该作业仍然无法运行。
任何想法?
谢谢。
编辑:根据@Raffa的评论
ls -l /home/user/folder/script.sh
输出:
-rwxrwxr-x 1 user user 585 Aug 8 06:51 20_run_commissioning.sh
journalctl --grep='cron' --no-pager --since='-1hour'
输出:
-- Logs begin at Thu 2023-08-10 05:28:46 UTC, end at Thu 2023-08-10 13:31:09 UTC. --
Aug 10 13:31:04 ubuntu cron[706]: (CRON) INFO (pidfile fd = 3)
Aug 10 13:31:04 uncommissioned cron[706]: (CRON) INFO (Running @reboot jobs)
Aug 10 05:39:09 uncommissioned systemd[1]: cron.service: Succeeded.
-- Reboot --
Aug 10 05:40:06 uncommissioned cron[728]: (CRON) INFO (pidfile fd = 3)
Aug 10 05:40:06 uncommissioned cron[728]: (CRON) INFO (Running @reboot jobs)
Aug 10 05:40:06 uncommissioned CRON[732]: pam_unix(cron:session): session opened for user user by (uid=0)
Aug 10 05:40:06 uncommissioned CRON[766]: (user) CMD (/home/user/commissioning/20_run_commissioning.sh >> /home/user/commissioning/cron.log 2>&1^M)
Aug 10 05:40:06 uncommissioned CRON[732]: (CRON) info (No MTA installed, discarding output)
Aug 10 05:40:06 uncommissioned CRON[732]: pam_unix(cron:session): session closed for user user
答案1
我找到了问题所在。这是在
Aug 10 05:40:06 uncommissioned CRON[766]: (user) CMD (/home/user/commissioning/20_run_commissioning.sh >> /home/user/commissioning/cron.log 2>&1^M)
在我的 Windows 机器上写入此文件。
感谢@Raffa的帮助