简体   繁体   English

为什么我的crontab无法正常工作?

[英]Why does my crontab not work?

I am planning to run some bash scripts every minute, and I wrote: 我打算每分钟运行一些bash脚本,我写道:

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

in crontab. 在crontab中。

It was supposed to run every minute, but it did not work. 它应该每分钟运行一次,但是没有用。 Does anyone have idea why this happens? 有谁知道为什么会这样?

Transferring a comment into an answer. 将评论转换为答案。

Add I/O redirection to the command line in the crontab entry: crontab条目中将I / O重定向添加到命令行:

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

Review the contents of the files after a minute or two has passed. 一两分钟后,请检查文件的内容。 Consider recording the environment to see if that's part of the problem. 考虑记录环境以查看是否是问题的一部分。 And consider using bash -x instead of just bash . 并考虑使用bash -x而不是bash

If you still don't get anything (the files in /tmp are not created), then you've got issues with cron ; 如果仍然什么也没得到( /tmp中的文件未创建),则说明cron问题; the daemon isn't running, or your user does not have permission to use it (but crontab isn't telling you that), or you've not submitted your crontab to the program (what does crontab -l say?), or … whatever is really wrong. 守护程序未运行,或者您的用户没有使用该守护程序的权限(但crontab并未告知您),或者您尚未将crontab提交给程序( crontab -l说什么?),或者……什么是真的错。

Note, too, that the output from cron jobs is normally (well, at least sometimes — on Mac OS X for a system I currently use, and Solaris for another that I've used previously) emailed to the person whose job it is. 同样要注意, cron作业的输出通常是(通过电子邮件发送给正在工作的人)(至少在某些情况下,在Mac OS X上是我当前使用的系统,在Solaris是我以前使用的另一个系统)。 You should review the email on the system. 您应该查看系统上的电子邮件。

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. 它不起作用的原因是我在脚本中使用了“ ls -a .sh ”,并且crontab在正在执行的文件夹中找不到任何*.sh文件时。 When modifying it to " ls -a $HOME/Dropbox/temp_scripts/.sh ", everything works! 将其修改为“ ls -a $HOME/Dropbox/temp_scripts/.sh ”时,一切正常! This debugging technique is quite helpful! 此调试技术非常有帮助!

It is, in many ways, the most basic of debugging techniques — make sure you see what is actually happening. 在许多方面,它是最基本的调试技术-确保您看到实际发生的情况。 If you're not sure why a shell script isn't working, make sure you can see that it is executing and what it is producing in the way of output, and (very often) make sure you can see what it is executing with bash -x or equivalent. 如果不确定为什么外壳脚本不起作用,请确保可以看到它正在执行以及它以输出方式产生了什么,并且(经常)确保您可以看到它在执行中的结果bash -x或等效的。 (AFAIK, all shells support -x to trace the execution.) (AFAIK,所有外壳程序都支持-x来跟踪执行。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM