2

I'm struggling trying to debug a cron job which isn't working correctly. The cron job calls a shell script which should unrar a rar file - this works correctly when i run the script manually, but for some reason it's not working via cron. I am using the absolute file path and have verified that the path is correct. Has anyone got any ideas why this could be happening?

4

3 回答 3

18

好吧,您已经说过您使用了绝对路径,所以解决了第一个问题。

接下来要检查的是权限。cron 作业以哪个用户身份运行?它是否具有所有必要的权限?

然后,一个小技巧:如果你有一个失败的 shell 脚本并且它没有在终端中运行,我喜欢将它的输出重定向到一些文件。在脚本的开头,添加:

exec &>/tmp/my.log

这会将 STDOUT 和 STDERR 重定向到/tmp/my.log. 然后添加以下行可能也是一个好主意:

set -x

这将使 bash 打印它即将执行的命令,以及在什么嵌套级别。

调试愉快!

于 2010-08-31T18:13:10.257 回答
2

The first thing to check when cron jobs fail is to see if the full environment is available to the script you are trying to execute. In other words, you need to realize that a job executed via cron runs as a detached process meaning it is not associated with a login environment. Therefore whenever you try to debug a cron job that works when you execute manually, you need to be sure the same environment is available to the cronjob as is available to you when you execute it manually. This include any PATH settings, and other envvars that the script may depend on.

于 2010-08-31T18:02:48.833 回答
0

对我来说,问题是 crontab 中的一个不同的 shell 解释器。

于 2013-08-29T09:58:19.097 回答