Cronjob does not work on rake command - ruby

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

Related

Execute symfony command in bash script

I can't get to execute symfony command in bash script when I run it in cron.
When I execute the .sh script by hand everything is working fine.
in my bash file the command is executed like this:
/usr/bin/php -q /var/www/pww24/bin/console pww24:import asari $office > /dev/null
I run the scripts from root, the cron is set to root as well. For the test i set files permissions to 777 and added +x for execution.
the bash script executes fine. It acts like it's skipping the command but from logs i can see that the code is executed
It turned out that symfony system variables that I have stored on server are not enough. When you start to execute the command from command line its fine, but when using Cron you need them in .env file. Turned out that in the proces of countinous integrations I only got .env.dist file and I've to make the .env file anyways.
Additionaly I've added two lines to cron:
PATH=~/bin:/usr/bin/:/bin
SHELL=/bin/bash
and run my command like this from the bash file:
sudo /usr/bin/php -q /var/www/pww24/bin/console pww24:import asari $office > /dev/null

Rake command not working in cron but works when running as command

I have placed the following in crontab (Executes for every minute)
* * * * * cd /home/foo/Projects/redmine-2-4-2-prod && /home/foo/.rvm/gems/ruby-2.0.0-p0/bin/rake RAILS_ENV=production --silent redmine:email:receive_imap host=imap.gmail.com port=993 ssl=1 username=foo#gmail.com password='foopass' project=testredmine tracker=Support status=New priority=High allow_override=project,status,tracker,priority
I see cron running every minute in logs (/var/log/syslog). On executing the above command in shell, I receive e-mails. But not receiving e-mails when the same command is executed in cron.
Please help me to know, what am I missing.
Try to add somethin like this to your cron task:
source /home/user/.rvm/environments/ruby-1.9.3
Path to your rvm files you can get by:
rvm env --path

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'

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

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.

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