Can the whenever gem preserve existing lines in a crontab file? - ruby

I am using:
Ruby 1.9.2
whenever 0.7.2
capistrano 2.9.0
capistrano-ext 1.2.1
I am using whenever in conjunction with Capistrano on deploys to manage my crontab files.
I noticed that it completely rewrites my crontab files each time.
I'd like to be able to set environment variables in cron to control PATH and MAILTO settings, which are regular cron environment variables.
Is there a way to make whenever not overwrite the entire crontab file, so that I can add customizations to my crontab file and be sure that they will persist?

Yes, you can do this. You'll just need to assign an identifier to the task being written to crontab:
whenever --update-crontab some_identifier_name
It will generate an entry in crontab like this:
# Begin Whenever generated tasks for: some_identifier_name
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /var/www/test/releases/20120416183153 && script/rails runner -e production '\''Model.some_method'\'' >> /tmp/cron_log.log 2>&1'
# End Whenever generated tasks for: some_identifier_name
Then whenever you call the command above it will only update where it finds the identifier you specified.

Related

Cronjob does not work on rake command

I am totally stuck on a cronjob command which does not want to work.
Here is my problem :
I just want to launch a rake command on my crontab (for Redmine : check all unread emails on a specific address through IMAP and create a redmine ticket)
Here is the command :
cd /opt/redmine/ && rake redmine:email:receive_imap RAILS_ENV="production" host=imap.gmail.com username=redmine.check#gmail.com ssl=true port=993 password=MyPassword project=level1support unknown_user=accept no_permission_check=1 allow_override=project
I am able to launch this on my command line and everything works fine
So I made a crontab -e and add this line on my crontab :
*/10 * * * * cd /opt/redmine/ && rake redmine:email:receive_imap RAILS_ENV="production" host=imap.gmail.com username=redmine.check#gmail.com ssl=true port=993 password=MyPassword project=level1support unknown_user=accept no_permission_check=1 allow_override=project
But it does not work.
The cron seems to run (it is OK in my /var/log/cron file)
I added a log file on this cron like this (at the end of the line) > /tmp/crontabRedmin.log 2&>1 but nothing is written on the log file.
I created a .sh script, I tried different syntaxes for the rake command (/usr/local/bin/rake or && RAILS_ENV=production bundle exec rake), everything works on my command line , but nothing through the cron.
Please help me to know, what am I missing.
When the rake task is executed using the cron job the .bash_profile file is not processed so you need to add the environment variables to the sell script.
Find the Ruby Gems path etc using the following:
echo $GEM_HOME
echo $GEM_PATH
echo $PATH
Add the following to your shell script:
#!/bin/bash
export PATH=$PATH:/home/user/.rvm/gems/ruby-2.0.0-p0/bin:/home/user/.rvm/gems/ruby-2.0.0-p0#global/bin:/home/user/.rvm/bin
export GEM_HOME=/home/user/.rvm/gems/ruby-2.0.0-p0
export GEM_PATH=/home/user/.rvm/gems/ruby-2.0.0-p0:/home/user/.rvm/gems/ruby-2.0.0-p0#global

Cron job can't load gem

I have a ruby script that connects to an Amazon S3 bucket and downloads the latest production backup. I have tested the script (which is very simple) and it works fine.
However, when I schedule this script to be run as a cron job it seems to fail when it loads the Amazon (aws-s3) gem.
The first few lines of my script looks like this:
#!/usr/bin/env ruby
require 'aws/s3'
As I said, when I run this script manually, it works fine. When I run it via a scheduled cron job, it fails when it tries to load the gem:
`require': no such file to load -- aws/s3 (LoadError)
The crontab for this script looks like this:
0 3 * * * ~/Downloader/download.rb > ~/Downloader/output.log 2>&1
I originally thought it might be because cron is running as a different user, but when I do a 'whoami' at the start of my ruby script it tells me it's running as the same user I always use.
I have also done a bundle init and added the gem to my gemfile, but this doesn't seem to have any affect.
Why does cron fail to load the gem? I am running Ubuntu.
As mentioned here https://coderwall.com/p/vhv8aw you can simply try
rvm cron setup # let RMV do your cron settings
Make sure that you make copy of your crontab before running this command
If you're running it manually and it works you're probably in a different shell environment than cron is executing in. Since you mention you're on Ubuntu, the cron jobs probably execute under /bin/sh, and you're manually running them under /bin/bash if you haven't changed anything.
You can debug your environment problems or you can change the shell that your job runs under.
To debug, There are several ways to figure out what shell your cron jobs are using. It can be defined in
/etc/crontab
or you can make a cron job to dump your shell and environment information, as has been mentioned in this SO answer: How to simulate the environment cron executes a script with?
To switch to that shell and see the actual errors causing your job to fail, do
sudo su
env -i <path to shell> (e.g. /bin/sh)
Then running your script you should see what the errors are and be able to fix them (rubygems?).
Option 2 is to switch shells. You can always try something like:
0 3 * * * /bin/bash -c '~/Downloader/download.rb > ~/Downloader/output.log 2>&1'
To force your job into bash. That might also clear things up.
You may also explicitly set your Gem path:
GEM_HOME="/usr/local/rvm/gems/ruby-1.9.2-p290#my-special-gemset"
in a non cron environment execute echo $PATH, copy the path and paste it into your crontab, before your command:
echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
and inside crontab:
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
0 3 * * * ~/Downloader/download.rb > ~/Downloader/output.log 2>&1
Add this at the beginning of your cron
PATH="/home/user/.rvm/gems/ruby-2.1.4/bin:/home/user/.rvm/gems/ruby-2.1.4#global/bin:/home/user/.rvm/rubies/ruby-2.1.4/bin:/home/user/.rvm/gems/ruby-2.1.4/bin:/home/user/.rvm/gems/ruby-2.1.4#global/bin:/home/user/.rvm/rubies/ruby-2.1.4/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/home/user/.rvm/bin:/usr/local/sbin:/usr/sbin:/home/user/.rvm/bin:/home/user/.local/bin:/home/user/bin"
GEM_HOME='/home/user/.rvm/gems/ruby-2.1.4'
GEM_PATH='/home/user/.rvm/gems/ruby-2.1.4:/home/user/.rvm/gems/ruby-2.1.4#global'
MY_RUBY_HOME='/home/user/.rvm/rubies/ruby-2.1.4'
IRBRC='/home/user/.rvm/rubies/ruby-2.1.4/.irbrc'
RUBY_VERSION='ruby-2.1.4'
I've tried all the solution above, none of them worked until I tried;
0 12 * * * /bin/bash -l -c 'ruby /Users/simon/Desktop/script.rb'

Setting path for whenever in cron so it can find ruby

My ruby is in /usr/local/bin. whenever can't find it, and setting PATH at the top of my cron file doesn't work either, I think because whenever is running the command inside of a new bash instance.
# this does not work
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin
# Begin Whenever generated tasks for: foo
0 * * * * /bin/bash -l -c 'cd /srv/foo/releases/20110429110637 && script/rails runner -e production '\''ActiveRecord::SessionStore::Session.destroy_recent(15)'\'''
# End Whenever generated tasks for: foo
How can I tell whenever where my ruby binary is? Making a symbolic link from /usr/bin seems messy to me, but I guess that might be the only option.
This question offers env :PATH, "..." in schedule.rb as a solution, but (a) I can't find any documentation of that feature anywhere in the docs (b) it doesn't seem to have solved the asker's problem (unfortunately it takes non-trivial turnaround time for me to just try it).
update actually it is in the bottom of this page, i'll try it now.
more info
I can't modify the cron command because it's generated by whenever
i verified that if I make a new bash shell with bash -l, /usr/bin/env finds ruby just fine
I just tried the exact command in cron, starting with /bin/bash, from the command line of that user, and it worked.
so, this is very mysterious...
The solution is to put this in schedule.rb:
env :PATH, ENV['PATH']
Here's a little guide I put together on the topic.
rewrite your crontab as
0 * * * * { PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin ; export PATH ;/bin/bash -l -c 'cd /srv/foo/releases/20110429110637 && script/rails runner -e production '\''ActiveRecord::SessionStore::Session.destroy_recent(15)'\''' ; }
Or you should try to figure out why your BASH shell is not picking the PATH=... that is almost certainly in your .profile or .bash_profile.
I hope this helps.
As John Bachir pointed out, you can do it via env. But let me add more input. I am deploying on AWS Opsworks. Unfortunately they do not have a ruby manager (RVM, Rbenv, etc) installed by default.
The first thing I needed to do was SSH into the instance and figure out which ruby I was using. This was easy enough by executing the which ruby command in a terminal.
$ which ruby
/usr/local/bin/ruby
Cron was using ruby located at /usr/bin/ruby. This needed to be changed.
In schedule.rb, I have:
set :env_path, ''
env :PATH, #env_path if #env_path.present?
In local, env_path doesn't need to be set. For most users, the only thing to do is execute whenever as such:
bundle exec whenever --set 'environment=development' --update-crontab
On a staging / production environment, ruby may be installed elsewhere. So running this may be more appropriate:
bundle exec whenever --set 'environment=staging&env_path=/usr/bin/local' --update-crontab
You will need to replace /usr/bin/local with the output of echo $PATH.
In Opsworks, however, I needed to create a custom Chef recipe that looked like:
node[:deploy].each do |application, deploy|
execute 'whenever' do
user 'deploy'
group 'nginx'
cwd "#{deploy[:deploy_to]}/current"
command "bundle exec whenever --set 'environment=#{deploy[:environment_variables][:RAILS_ENV]}&env_path=#{ENV['PATH']}' --update-crontab"
end
end
I hope the information here is clear enough.

Cron Ubuntu does not fire ruby method

crontab -l gives me this
0,2,4,6,8,10 * * * * /bin/bash -l -c 'cd /home/ruben/Monitoring ; script/rails runner Ping.check_pings'
Why does this not work?
If i try "cd /home/ruben/Monitoring ; script/rails runner Ping.check_pings" in the command line it works. I have also tried it with "&&" as ";"
The problem may be related to PATH, or to some other environment variable (like GEM_HOME), that is defined properly in your command-line environment, but not in cron's environment.
crontab doesn't run with the enviroment of the user, rather it creates it's own slimmed down enviroment. This includes very small PATH - /usr/bin:/usr/sbin:. and some other variables. See more here - http://adminschoice.com/crontab-quick-reference
Easiest solution is to add '. ~/.profile' before you run rails, or to fix path in some other way.
BTW, before you try to add PATH=/my/path/here;$PATH into crontab - that syntax (variable expansion) is not allowed either

Creating crontab via Capistrano instead of using crontab -e

I would like to include cron tasks in my Capistrano deployment files instead of using the following command to manually edit the crontab file:
crontab -e [username]
Is there a script I could use within the Capistrano run command to set the contents of the crontab?
Check out the Whenever gem -- this may be stretching beyond what you're intending to do, but it uses very simple (Ruby) syntax and makes it dead simple to setup cron jobs within a capistrano deployment script.
On my linux box
crontab -u userName -l > fileName
lists the crontab file for userName in fileName.
Then I would use a ruby (or another language) script to update the file.
Finally I would use
crontab -u userName fileName
to update the crontab for userName
given that you have a variable set that is :new_user
and that you are using use_sudo true
desc "install crontab"
task :install_crontab do
run "echo '0 23 * * * /home/#{new_user}/scripts/backup_#{new_user}.sh' | #{sudo} crontab -u #{new_user} -"
end
def crontab_add(line)
config = capture(%Q{crontab -l}).split "\n"
return if config.include? line
run %Q{(crontab -l; echo "#{line}") | crontab -}
end
Why not include a crontab that can be installed to /etc/cron.d?

Resources