为什么我的 crontab 不工作?

Why does my crontab not work?

我计划每分钟 运行 一些 bash 脚本,我写道:

* * * * * bash ~/Dropbox/temp_scripts/run_all_scripts 

在 crontab 中。

本来应该每分钟 运行,但没有用。有谁知道为什么会这样?

正在将评论转为答案。

crontab 条目中添加 I/O 重定向到命令行:

>/tmp/run_all_scripts.out 2>/tmp/run_all_scripts.err

一两分钟后查看文件内容。考虑记录环境以查看这是否是问题的一部分。并考虑使用 bash -x 而不是 bash.

如果您仍然没有得到任何东西(未创建 /tmp 中的文件),那么您遇到了 cron 的问题;守护程序不是 运行,或者您的用户没有使用它的权限(但 crontab 没有告诉您),或者您没有将 crontab 提交给程序(crontab -l 说的是什么?),或者……任何真正错误的东西。

还要注意,cron 作业的输出通常是(好吧,至少有时 — 在 Mac OS X 上我目前使用的系统,Solaris另一个我以前用过的)通过电子邮件发送给工作的人。您应该查看系统上的电子邮件。

Thank you! I have already fixed it! The reason why it does not work is I used "ls -a .sh" in the script, and when the crontab did not find any *.sh files in the folder it was executing. When modifying it to "ls -a $HOME/Dropbox/temp_scripts/.sh", everything works! This debugging technique is quite helpful!

在很多方面,它是最基本的调试技术 — 确保您看到实际发生的情况。如果您不确定为什么 shell 脚本不工作,请确保您可以看到它正在执行以及它以输出方式产生的内容,并且(经常)确保您可以看到什么它正在执行 bash -x 或等效的。 (据我所知,所有 shell 都支持 -x 来跟踪执行。)