Crontab not running ruby script
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.
how to create a cron job to run a ruby script?
If your ruby is in non standard paths then personally I like to wrap my ruby calls in a shell script, thereby ensuring that all the paths etc. my ruby program needs are set correctly, and schedule the script in crontab. Do something like
2 * * * * /home/mark/project/ruby_wrapper_sh >> /home/mark/cronOutput.txt 2>&1
and your /home/mark/project/ruby_wrapper_sh should read something like #!/bin/bash
. ~mark/.bash_profile
`ruby /home/mark/project/script.rb`
ruby script cron not working
This has nothing to do with cron, actually. ~
is a feature of the shell, Ruby doesn't know what it is. Only some methods in Ruby deal with ~
, e.g. File::expand_path
. So, Ruby is literally trying to open a file named newfile.txt
in directory named ~
in the current working directory. Try it out yourself: create an empty directory named ~
in your home directory, and sure enough, after 1 minute, you should find a new file named newfile.txt
with the content hi..
in there.
There are a couple of ways to fix this, I will leave it up to you which one to use:
File.open(File.expand_path('~/newfile.txt'), 'a') do |f| end
File.open(File.join(Dir.home, 'newfile.txt'), 'a') do |f| end
[Note: if you only want to append to the end of the file, not move around or read, a
is enough, you don't need a+
.]See the documentation for File::expand_path
(bold emphasis mine):
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless
expand_path(file_name [, dir_string] )
→abs_file_name
dir_string
is given, in which case it will be used as the starting point. The given pathname may start with a “~”, which expands to the process owner’s home directory (the environment variableHOME
must be set correctly). “~user” expands to the named user’s home directory.
Crontab execute the command but the ruby script is not running
Try:
12 10 * * * /bin/bash -c 'source /home/user/.rvm/scripts/rvm && cd /home/user/path/ && /home/user/.rvm/bin/ruby-1.9.2-p136 -rubygems script.rb'
Cron won't execute ruby script
In your question, the cron you are running goes like
2 * * * * . $HOME/.bash_profile ~/jek.sh
There are multiple things you need to correct/verify with this entry- Check that file permissions for both is
777
or775
(rwx
). If not, then change the file permission usingchmod 777 ~/jek.sh
- Check that each of them define in their shebang line which language script they are (
#!/usr/local/env sh
) - Separate the two scripts by an
&&
or;
so that both of them are run properly. Currently, the second script's name will be treated as a parameter for the first. - There is a
.
after the2 * * * *
part. I am not sure why you added it - it has to be removed.
In case @psny's answer doesn't work for you, try exporting your path variable in your cron entry. After that, the whole thing should work properly. Steps
1) Find the value of $PATH
echo $PATH #Lets call the string :some/path/:another/path
2) Manually set the path in your crontab entry2 * * * * export PATH=:some/path/:another/path && /bin/bash /home/username/jek.sh
Related Topics
How to Create a Custom Sort Method in Ruby
How to "Break" Out of a Case...While in Ruby
Problems While Making a Generic Model in Ruby on Rails 3
Timeout When Installing Ruby Gems
Camelcase Instead of Snake_Case in Rails Db
How to Show Longer Traces in Rails Testcases
Paperclip: Integration with Mailboxer Gem
What Is Toplevel_Binding in Ruby
Use a String to Access a Local Variable by Name
How to Validate This Hash Parameter
How to Convert a Fraction to Float in Ruby
How to Share Code Across Chef Cookbooks in a Chef-Repo
Is 'Respond_To_Missing''s Second Argument Useful for Anything
How to Open File in Default Application. Ruby
Ruby Fails on Osx Lion with Rbenv
Ruby - Generate All Two Letter Words
Best/Most Elegant Way to Share Objects Between a Stack of Rack Mounted Apps/Middlewares