Crontab未运行Ruby脚本 [英] Crontab not running ruby script

查看:107
本文介绍了Crontab未运行Ruby脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的crontab -l似乎没有运行。手动运行的脚本运行良好。这是我看到的错误

The crontab -l below doesn't seem to run. The script run by hand runs fine. Here is the error i'm seeing

Dec  3 20:12:01 dahlia /USR/SBIN/CRON[13912]: (gigawatt) CMD (/bin/sh -c "(export   PATH=/usr/bin:/bin; /home/gigawatt/drbronnersbot/drbronnersbot.rb)")
Dec  3 20:12:01 dahlia /USR/SBIN/CRON[13910]: (CRON) error (grandchild #13912 failed with exit status 1)

这是crontab:

* * * * * /bin/sh -c "(export PATH=/usr/bin:/bin; /home/gigawatt/drbronnersbot/drbronnersbot.rb)"

权限已完全打开,其可执行文件,我将环境路径放在文件的开头,仍然没有骰子。

Permissions are fully open, its executable, i put the env path at the beginning of the file, still no dice.

推荐答案

编辑:我只是注意到用户回答了自己的帖子,但我将保留此内容以防有人偶然遇到类似问题。我发现它对某些人有用。

Edit: I just noticed the user answered his own post, but I'll leave this up in case someone stumbles across here with a similar issue. I've found it's helpful for some.

我遇到了这个问题,发现这是Ruby脚本的解决方案。

I've run into this and found that this is the solution for Ruby scripts.

Ruby需要在特定环境中执行。 RVM通过为您的特定版本的ruby采购ruby环境文件来解决此问题,该文件设置了所有必需的环境变量。例如,如果您具有ruby 1.9.3补丁448,则可以查看来源的环境文件:

Ruby needs to be executed in a particular environment. RVM handles this by sourcing a ruby environment file for you particular version of ruby that sets all the required environment variables. For example if you have ruby 1.9.3 patch 448, you can look at the environment file that is sourced:

cat /usr/local/rvm/environments/ruby-1.9.3-p484

export PATH="/usr/local/rvm/gems/ruby-1.9.3-p484/bin:/usr/local/rvm/gems/ruby-1.9.3-p484@global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p484/bin:$PATH"
export GEM_HOME='/usr/local/rvm/gems/ruby-1.9.3-p484'
export GEM_PATH='/usr/local/rvm/gems/ruby-1.9.3-p484:/usr/local/rvm/gems/ruby-1.9.3-p484@global'
export IRBRC='/usr/local/rvm/rubies/ruby-1.9.3-p484/.irbrc'
unset MAGLEV_HOME
unset RBXOPT

(注意:我的rvm安装在 / usr / local /..,但您可能在其他地方。使用哪个红宝石来确定您的红宝石的安装位置)

(Note: My installation of rvm was under /usr/local/.. but yours may be elsewhere. Use which ruby to figure out where your ruby is installed)

在这里您可以看到它正在设置 PATH 和其他一些重要的环境变量。当您输入 rvm时,使用ruby-1.9.3-p448 ,rvm实际上会在后台提供该文件。

Here you can see that it's setting the PATH and some other important environment variables. When you type rvm use ruby-1.9.3-p448, rvm actually sources this file in the background.

Cron执行是非交互式会话,这意味着它们在会话之前没有实时用户登录。当您手动执行并运行交互式会话时,所有这些工作都将由您解决,但是对于非交互式会话,它不知道外壳是什么或在哪里可以找到环境路径。也许有更多知识的人可以提供为什么的技术解释。

Cron executions are non-interactive sessions, which means they have no "live" user logged-in in front of a session. When you do it manually and run an interactive session, all this is taken care of for you, but for a non-interactive session, it doesn't know what the shell is or where to find the environment path. Maybe someone with more knowledge can provide a technical explanation as to why.

无论如何,要解决此问题,请将其添加到crontab的顶部:

Anyways, to get around that, add this to the top of your crontab:

SHELL=/bin/bash
BASH_ENV=/home/gigawatt/.bashrc

* * * * * /home/gigawatt/.rvm/rubies/ruby-2.0.0-p247/bin/ruby /home/gigawatt/drbronnersbot/drbronnersbot.rb

这是告诉非交互式cron用户要使用哪个shell,然后告诉其来源 .bashrc 文件。 .bashrc 文件中包含什么?好问题,您应该添加以下行-

This is telling the non-interactive cron user which shell to use and then telling it to source the .bashrc file. What's in the .bashrc file? Good question, you should add this line -

source /usr/local/rvm/environments/ruby-1.9.3-p484

(再次,用您自己的红宝石路径替换)
基本上,您是在手动采购rvm为您提供的环境文件。这是让cron使用特定的gem环境或gemset的一种方法。

(Once again, replace with your own ruby path) Basically you are manually sourcing the environment file that rvm would have sourced for you. It's a way of getting cron to use a particular gem environment or gemset.

应该对这两项更改起作用。

It should work with those two changes.

Edit2 :在Ubuntu和类似系统上,默认的 .bashrc 通常包含

Edit2: On Ubuntu and similar systems, the default .bashrc often contains something like

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

正如注释所暗示的,文件不会运行在非交互/定时会议中。在该行下添加的所有内容都将不会执行。

As the comment implies, the file wont run in a non-interactive/cron session. Anything you add under that line won't be executed.

在这种情况下,请将 source 命令放在上方或仅使用其他文件,例如〜/ .cronrc

In that case, either put your source command above that line or just use a different file all together, like ~/.cronrc for example.

这篇关于Crontab未运行Ruby脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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