Crontab not working ruby ​​script

The following is an example of crontab -l. The script operation is performed manually. Here is the error I see

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 crontab:

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

Permissions are fully open, its executable file, I put the env path at the beginning of the file, still not playing dice.

+8
ruby crontab
source share
5 answers

Edit : I just noticed that the user replied to his own post, but I will leave it if someone encounters a similar problem. I found this helpful for some.

I ran into this and found that this is a solution for Ruby scripts.

Ruby must be run in a specific environment. RVM solves this problem by searching for the ruby ​​environment file for a specific version of ruby, which sets all the necessary environment variables. For example, if you have patch 448 for ruby ​​1.9.3, you can look at the environment file that was received:

 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 rvm installation was in /usr/local/.. but your installation may be in a different place. Use which ruby to find out where your ruby ​​is installed)

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

Cron runs are non-interactive sessions, which means that they do not have a “live” user logged in before the session. When you do this manually and start an interactive session, all this will take care of you, but for a non-interactive session, he does not know what the shell is or where to find the path to the environment. Maybe someone with more knowledge can give a technical explanation of why.

In any case, to get around this, 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 tells the non-interactive cron user which shell to use, and then tells the source .bashrc . What is in the .bashrc ? Good question, you should add this line -

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

(Replace your own ruby ​​path again). Basically, you manually select the environment file that rvm might find for you. This is a way to get cron to use a specific gem environment or gemset.

It should work with these two changes.

Edit2 : on Ubuntu and similar systems by default .bashrc often contains something like

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

As the commentary suggests, the file will not run in a non-interactive / cron session. Anything you add below this line will fail.

In this case, either place the source command above this line, or simply use another file together, for example, ~/.cronrc .

+10
source share

I started working using

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

+9
source share

Solved this, I called two files in my code, and he could not find these files. I am editing a .rb file to include the full path, and now it works fine. Thanks everyone!

+2
source share

It's pretty old, but I will comment anyway. RVM creates a wrapper for each version of ruby ​​you install.

run this command replace 2.5.1 with your version of ruby

 rvm env --path 2.5.1 

it gives you a way to the environment

/home/username/.rvm/environments/2.5.1

Do you want just the RVM path

/home/username/.rvm/

take a look at the wrappers directory, there should be a shell for each version you install and a set of gems. Test your script with a wrapper

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

or if you have a certain gem

 /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 
+2
source share

You tried:

 * * * * * /home/gigawatt/.rvm/rubies/ruby-2.0.0-p247/bin/ruby /home/gigawatt/drbronnersbot/drbronnersbot.rb 
+1
source share

All Articles