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

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

问题描述

我有一个Rails脚本,我想每天运行。我知道有很多方法,一些cron'd 脚本/ runner 方法被一些人皱眉,但似乎满足了我的需要。

$ b



我的应用程序生活在 / data / myapp /当前,脚本位于 script / myscript.rb 中。我可以手动运行它,因为 root 与:

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

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

  Tue Mar 03 13:16:00 -0500 2009开始执行脚本... 
...
Tue Mar 03 13:19:08 -0500 2009完成执行脚本在188.075028秒

我设置为使用 每天早上4点。 root 的crontab:

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

事实上,它看起来像今天早上一样运行。

  $ tail -100 / var / log / cron 
...
Mar 2 04:00:01 hostname crond [8894]:(root)CMD(/ data / myapp / current / script / runner -e生成/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)
...

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

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

我在CentOS 5上运行。



所以我的问题是...


  1. 在哪里可以找到调试此信息?

  2. 这可能是一个SELinux问题吗?是否有可以设置或更改的安全上下文可以解决此错误?

谢谢!



更新



感谢Paul和Luke。它确实是一个环境问题,捕获 stderr 到日志文件使我找到错误。

  $ cat cron.log 
/ usr / bin / env:ruby:没有这样的文件或目录

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

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

  $ 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将其输出发送给运行它的用户。



重定向由cron运行的脚本的输出非常有用,这样您可以查看日志文件中的结果,而不是某些随机用户的本地



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

  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 

code>>> 将stdout重定向到文件,并且 2>& 1 将stderr重定向到stdout,



完成此操作后,您可以检查错误消息,以了解实际发生的情况。


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.

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

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

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
...

I am running on CentOS 5.

So my questions are...

  1. Where else can I look for information to debug this?
  2. Could this be a SELinux issue? Is there a security context that I could set or change that might resolve this error?

Thank you!

Update

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'

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

解决方案

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

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.

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

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天全站免登陆