如何调试 cron 未执行给定脚本或其他脚本的问题? [英] How to debug an issue of cron's not executing a given script -- or other?

查看:32
本文介绍了如何调试 cron 未执行给定脚本或其他脚本的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个希望每天运行的 Rails 脚本.我知道有很多方法,并且 cron'd script/runner 方法被某些人所反对,但它似乎满足了我的需求.

I have a Rails script that I would like to run daily. I know there are many approaches, and that a cron'd script/runner approach is frowned upon by some, but it seems to meet my needs.

但是,我的脚本没有按计划执行.

However, my script is not getting executed as scheduled.

我的应用程序位于 /data/myapp/current,脚本位于 script/myscript.rb 中.我可以使用 root 手动运行它:

My application lives at /data/myapp/current, and the script is in script/myscript.rb. I can run it manually without problem as root with:

/data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb

当我这样做时,特殊日志文件 (log/myscript.log) 会按预期记录到:

When I do that, the special log file (log/myscript.log) gets logged to as expected:

Tue Mar 03 13:16:00 -0500 2009 Starting to execute script...
...
Tue Mar 03 13:19:08 -0500 2009 Finished executing script in 188.075028 seconds

我将它设置为每天早上 4 点使用 cron 运行.root 的 crontab:

I have it set to run with cron every morning at 4 am. root's crontab:

$ crontab -l
0 4 * * * /data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb

事实上,它看起来就像在今天早上之前尝试运行的一样!

In fact, it looks like it's tried to run as recently as this morning!

$ tail -100 /var/log/cron
...
Mar  2 04:00:01 hostname crond[8894]: (root) CMD (/data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb)
...
Mar  3 04:00:01 hostname crond[22398]: (root) CMD (/data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb)
...

但是,我的日志文件中没有条目,并且它应该更新的数据也没有得到更新.日志文件权限(作为测试)甚至设置为全局可写:

However, there is no entry in my log file, and the data that it should update has not been getting updated. The log file permissions (as a test) were even set to globally writable:

$ ls -lh
total 19M
...
-rw-rw-rw- 1 myuser apps 7.4K Mar  3 13:19 myscript.log
...

我在 CentOS 5 上运行.

I am running on CentOS 5.

所以我的问题是......

So my questions are...

  1. 我还可以在哪里查找信息以进行调试?
  2. 这可能是 SELinux 问题吗?是否有我可以设置或更改的安全上下文来解决此错误?

谢谢!

更新

感谢保罗和卢克.结果确实是环境问题,将 stderr 捕获到日志文件使我能够找到错误.

Thank you to Paul and Luke both. It did turn out to be an environment issue, and capturing the stderr to a log file enabled me to find the error.

$ cat cron.log 
/usr/bin/env: ruby: No such file or directory

$ head /data/myapp/current/script/runner 
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require 'commands/runner'

将特定的 Ruby 可执行文件添加到命令中就可以了:

Adding the specific Ruby executable to the command did the trick:

$ crontab -l
0 4 * * * /usr/local/bin/ruby /data/myapp/current/script/runner -e production /data/myapp/current/script/myscript.rb >> /data/myapp/current/log/cron.log 2>&1

推荐答案

默认情况下,cron 会将其输出邮寄给运行它的用户.你可以看看那里.

By default cron mails its output to the user who ran it. You could look there.

重定向由 cron 运行的脚本的输出非常有用,这样您就可以在日志文件中查看结果,而不是在服务器上查看一些随机用户的本地邮件.

It's very useful to redirect the output of scripts run by cron so that you can look at the results in a log file instead of some random user's local mail on the server.

以下是将 stdout 和 stderr 重定向到日志文件的方法:

Here's how you would redirect stdout and stderr to a log file:

cd /home/deploy/your_app/current; script/runner -e production ./script/my_cron_job.rb >> /home/deploy/your_app/current/log/my_file.log 2>&1

>> 将标准输出重定向到文件,2>&1 将标准错误重定向到标准输出,因此也会记录任何错误消息.

The >> redirect stdout to a file, and and the 2>&1 redirects stderr to stdout so any error messages will be logged as well.

完成此操作后,您将能够检查错误消息以了解实际情况.

Having done this, you will be able to examine the error messages to see what's really going on.

这篇关于如何调试 cron 未执行给定脚本或其他脚本的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆