RVM isnt setting environment with cron - ruby-on-rails

I'm having a rough time executing script/runner with a cron and RVM. I believe the issues lie with the rvm environment not being set before the runner is executed.
currently im throwing the error
/bin/sh: 1.sql: command not found
which is more than i've gotten earlier, so i guess that's good.
I've read this thread Need to set up rvm environment prior to every cron job but im still not really getting it. Part of the problem i think is the error reporting.
this is my runner thus far.
*/1 * * * * * /bin/bash -l -c 'rvm use 1.8.7-p352#2310; cd development/app/my_app2310 && script/runner -e development "Mailer.find_customer"'
as per the above link, i tried making a rvm_cron_runner.
i created a file and placed this in it:
#!/bin/sh
source "/Users/dude/.rvm/scripts/rvm"
exec $1
then i updated my crontab to this.
*/1 * * * * * /bin/bash -l -c '/Users/dude/development/app/my_app2310/rvm_cron_runner; rvm use 1.8.7-p352#2310; cd development/app/my_app2310 && script/runner -e development "Mailer.find_customer"'
This also has made no difference. i get no error. nothing.
Can anyone see what i'm doing incorrectly?
P.S i hope my code formatting worked.

Could you try to place the code you want to run in a separate script, and then use the rvm_cron_runner ?
So place your actions in a file called /path/cron_job
rvm use 1.8.7-p352#2310
cd development/app/my_app2310 && script/runner -e development "Mailer.find_customer"
and then in your crontab write
1 2 * * * /path/rvm_cron_runner /path/cron_job
The differences:
this does not start a separate shell
use the parameter of the rvm_cron_runner
If you would use an .rvmrc file, you could even drop the rvm use ... line, I think.

You don't need to write a second cron runner (following that logic, you might as well write a third cron runner runner). Please keep things simple. All you need to do is configure your cron job to launch a bash shell, and make that bash shell load your environment.
The shebang line in your script should not refer directly to a ruby executable, but to rvm's ruby:
#!/usr/bin/env ruby
This instructs the script to load the environment and run ruby as we would on the command line with rvm loaded.
On many UNIX derived systems, crontabs can have a configuration section before the actual lines that define the jobs to be run. If this is the case, you would then specify:
SHELL=/path/to/bash
This will ensure that the cron job will be spawned from bash. Still, your environment is missing, so to instruct bash to load your environment, you will want to add to the configuration section the following:
BASH_ENV=/path/to/environment (typically .bash_profile or .bashrc)
HOME is automatically derived from the /etc/passwd line of the crontab owner, but you can override it.
HOME=/path/to/home
After this, a cron job might look like this:
15 14 1 * * $HOME/rvm_script.rb
What if your crontab doesn't support the configuration section. Well, you will have to give all the environment directives in one line, with the job itself. For example,
15 14 1 * * export BASH_ENV=/path/to/environment && /full/path/to/bash -c '/full/path/to/rvm_script.rb'
Full blog post on the subject

You can use rvm wrappers:
/home/deploy/.rvm/wrappers/ruby-2.2.4/ruby
Source: https://rvm.io/deployment/cron#direct

Related

bundle command not found on bash script

I'm wrote a script to automatically run when reboot on crontab
this is my configuration in crontab -e
#reboot /home/deploy/startup_script >> /home/deploy/startup_script.log 2>$1
This start the script and create logs in /home/deploy
Then this is the startup_script
#!/bin/bash
echo "Changing directory"
cd /home/deploy/source/myapp
echo $PWD
echo "Pulling Dev Branch..."
git pull origin dev_branch
echo "Running Bundle Install"
sudo gem install bundler
bundle install
echo "Deploying to Staging..."
bundle exec cap staging deploy
when I run this script manually using ./startup_script it runs properly but when I run it automatically in crontab it shoes bundle command not found even I install the bundler already.
Here's the logs from startup_script.log
Changing directory
/home/deploy/source/myapp
Pulling Dev Branch...
From ssh://1.xx.xx.xx.io:20194/xx/myapp
* branch dev_branch -> FETCH_HEAD
Already up-to-date.
Running Bundle Install
Successfully installed bundler-2.0.2
Parsing documentation for bundler-2.0.2
Done installing documentation for bundler after 5 seconds
1 gem installed
/home/deploy/startup_script: line 12: bundle: command not found
Deploying to Staging...
/home/deploy/startup_script: line 15: bundle: command not found
The cron often clears the whole environment, including this $PATH variable. Therefore, the script may behave differently in your cron compared to the behavior in the shell. To avoid having to type the absolute path to a command, shells introduced the $PATH environment variable, each directory is separated by a : and searches are done from left to right.
Option I: You can use absolute path:
Run which bundle as sudoer to get the full path for the bundle command. If the output is /usr/bin/bundle, your bundle command in the script would look like:
/usr/bin/bundle install
Option II: Set the PATH variable:
Run echo "$PATH" as user who runs this script to get the $PATH variable and make sure this variable is available in your cron script too. For example, if the output was /usr/local/bin:/usr/bin:/bin, you would put the below line in the top of your shell script:
export PATH="/usr/local/bin:/usr/bin:/bin"
The environment that your crontab uses is going to be different than your regular login shell.
Now, I might be wrong about this, but I think when the crontab executes, it's not a login shell, so it doesn't have anything you've added to your path in your .bashrc or .bash_profile.
The best practice here would be to use the full path of the executable for bundle.
Redirecting stderr to stdout, there should be 2>&1
Is the path where the gem packages are installed is added to the $PATH variable? Try to provide the full path to this script
I suggest you make an entry to see what environment variables you have for crontab:
* * * * * printenv > ~/printenv.log

bundle exec not working with crontab

I'm trying to execute the following shell script using crontab:
#!/bin/sh
cd /mnt/voylla-production/current
bundle exec rake maintenance:last_2_days_orders
bundle exec rake maintenance:send_last_2_days_payment_dropouts
The crontab entry is
0 16 * * * /mnt/voylla-production/releases/20131031003111/voylla_scripts/cj_4pm.sh
I'm getting the following error message in the mail:
/mnt/voylla-staging/current/voylla_scripts/cj_4pm.sh: line 3: bundle: command not found
/mnt/voylla-staging/current/voylla_scripts/cj_4pm.sh: line 4: bundle: command not found
I dont get the error when I run the commands manually. Not sure what's going on here. Could someone please point out.
Thanks
A nice trick to get all environment properly set up in crontab is to use /bin/bash -l :
0 16 * * * /bin/bash -l -c '/mnt/voylla-production/releases/20131031003111/voylla_scripts/cj_4pm.sh'
The -l option will invoke a full login shell, thus reading your bashrc file and any path / rvm setting it performs.
If you want to simplify your crontab management and use this trick - as well as others - without having to think about them, you can use the Whenever gem. It also play very nice with capistrano, if you use it, regenerating crontab on deploy.
The user used by cron does not have the correct environment.
You can tell cron which user to use. For a bash script, you can so something like:
#!/bin/bash --login
source /home/user/.bashrc
rvm use 2.0.0#gemset #if you use rvm
cd /path/to/project && bundle exec xyz
We need set right path to our bundle:
#!/bin/sh
cd /mnt/voylla-production/current
/home/youruser/.rbenv/shims/bundle exec rake maintenance:last_2_days_orders

Whenever / cron jobs failing, but fine manually

Struggling with cron jobs. Ubuntu 11.10 on the server.
Until recently had whenever cron jobs running successfully several times a day; then due to another problem I had to remove RVM from the server and go back to ruby 1.9.3 installed without RVM (I'm sure this is something to do with it)
There is no .rvmrc file in my app
Now, the cron jobs are somehow failing as I can see from syslog:
Jun 30 08:03:01 ip-10-251-30-96 CRON[18706]: (ubuntu) CMD (/bin/bash -l -c 'cd /var/www/my_app/app/releases/201300629090954 && script/rails runner -e production '\''User.remind_non_confirmed_users'\''')
Jun 30 08:03:01 ip-10-251-30-96 CRON[18705]: (CRON) error (grandchild #18706 failed with exit status 127)
Jun 30 08:03:01 ip-10-251-30-96 CRON[18705]: (CRON) info (No MTA installed, discarding output)
If I run that command manually (with env - /bin/bash -l -c '...' ) it runs fine..
I'm going to add "set :output, 'tmp/whenever.log'" to whenever to see what is going on, but I suspect it is an issue with the ruby version / path or something.
Any idea how I could diagnose / fix this properly??
this is my cron/whenever job:
3 8 * * * /bin/bash -l -c 'cd /var/www/my_app/app/releases/20130629090954 && script/rails runner -e production '\''User.remind_non_confirmed_users'\'''
many thanks
To help diagnose what's going on, I usually capture the cron output into a separate log file. There's probably an error that's just not being recorded anywhere.
#hourly bash -lc 'cd /path/to/app; RAILS_ENV=production bundle exec rake remind_non_confirmed_users' >> /path/to/app/log/tasks.log
Also, I prefer creating rake tasks for cron jobs as opposed to runners. A little easier to invoke via the command line than runners, for me at least.
I'm still not sure what was going on, running Whatever with 'set :output' should have created log files, but it didn't, yet the jobs are still failing (and write permissions were there for the log files).
I got so fed up I redeveloped the solution without using script/runner, in stead have cron just call a URL that then takes care of matters as a delayed job. For our particular situation this has a number of additional benefits, though I know it is not ideal for many.
thanks for the suggestions

how to create a cron job to run a ruby script?

I want to create a cron job to run a ruby script. this is what i have put in the crontab.
2 * * * * ruby /home/mark/project/script.rb >> /home/mark/cronOutput.txt
But its not running. I think there's some problem with the environment getting loaded up when the cron runs as root.
Please help.
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`
If you are using RVM, you can simply do:
rvm cron setup
Reference:
https://coderwall.com/p/vhv8aw/getting-ruby-scripts-working-with-bundler-rvm-and-cron
Check whenever(https://github.com/javan/whenever) gem to use cron jobs in Rails
working on centos
in your terminal execute
# which ruby which is find your ruby path
example output
/usr/bin/ruby
Then you can edit your cronjob, using crontab -e
* * * * * /usr/bin/ruby /home/mark/project/script.rb
and save, this simply working on my centos server.
You can test the code first using this command before you edit your cronjob
#/usr/bin/ruby /home/mark/project/script.rb
it should be working first, then you can put on your crontab
Thanks for #saihgala for his solution, but I'm little modified this way.
I add #!/usr/bin/env ruby sting to the beginning of my ruby executable file.
Add permissions for this file, launch crontab file edit crontab -e.
Add */1 * * * 0-5 /path/to/executable.rb>>/path/to/output.txt and then it works for me.

Cron job can't get it to run, What syntax to use for the crontab?

I'm trying to get a rails job running with CRON. All the examples I find direct me to other rails tools, plugins, gems, etc, which is good, but I really just want to use CRON, regardless. I can run my job ok with the following, but when I've tried cron I haven't had any luck (just doesn't seem to do anything). I want to run it every 3 minutes (for testing).
/usr/bin/env ruby ~/Dropbox/98_2011/webs/apps238/swapper/script/runner /home/durrantm/Dropbox/98_2011/webs/apps238/swapper/app/controllers/scheduled_emails_controller.rb
I'm on Linux Ubuntu.
My PATH has:
/var/lib/gems/1.8/bin:/home/durrantm/.rvm/gems/ruby-1.8.7-p302/bin:/home/durrantm/.rvm/gems/ruby-1.8.7-p302#global/bin:/home/durrantm/.rvm/rubies/ruby-1.8.7-p302/bin:/home/durrantm/.rvm/bin:/home/durrantm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/pgsql/bin
Cron jobs don't load the user's environment. Try adding RAILS_ENV=production before your command within crontab, or whichever environment you need.
Example:
RAILS_ENV=production
*/3 * * * * /your/command/here
OR, if you want to make sure you have your user's full environment, execute the command within a login shell:
*/3 * * * * bash --login -c '/your/command/here'
Get rid of the home dir expansion character (replace ~ with /home/user/etc/etc) and you should be in good shape (quite likely cron's expansion of ~ doesn't match your users).
If the other parts of the syntax are bothersome there's an easy gui.
http://gnome-schedule.sourceforge.net/
sudo apt-get install gnome-schedule
You'll still have to have the path to your rb file fixed up though.
1- you might not have permissions. try running crontab -e as root
2- why don't you write to a log file to debug the issue:
*/3 * * * * /your/command/here >> /path/to/logfile

Resources