Crontab not running ruby script

半腔热情 提交于 2020-01-01 04:57:15

问题


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)

And here is the 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.


回答1:


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.

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

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

(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)

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

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

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

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

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




回答2:


I've gotten it working using

* * * * * /bin/bash -l -c 'ruby my-ruby-file.rb'




回答3:


This is quite old but thought I'd comment anyway. RVM creates a wrapper for each version of ruby you install.

run this command replace 2.5.1 with your ruby version

rvm env --path 2.5.1

this gives you the path to the environment mine is

/home/username/.rvm/environments/2.5.1

you want just the rvm path

/home/username/.rvm/

take a look at the wrappers directory there should be a wrapper for each version and gemset you have installed. Test your script with the wrapper

/home/username/.rvm/wrappers/ruby-2.5.1/ruby /home/username/scripts/script.rb

or if you have a specific gemset

/home/username/.rvm/wrappers/ruby-2.5.1@gemset/ruby /home/username/scripts/script.rb

Use this command directly in your crontab

* * * * * /home/username/.rvm/wrappers/ruby-2.5.1/ruby /home/username/scripts/script.rb



回答4:


Solved it, i was calling two files in my code and it couldn't find those files. I editing the .rb file to include the full path and it works perfectly now. Thanks everyone!




回答5:


Have you tried:

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


来源:https://stackoverflow.com/questions/20366718/crontab-not-running-ruby-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!