Search code examples
rubyrakecron

Rake: command not found when running cron job


I am running a Ruby app and every minute a script needs to run so that it sends emails out to various users.

The script works when I run it manually, however when running through cron it fails with the error "Rake: command not found".

I'm pretty new to all of this stuff and I know it will be something pretty basic but I can't find anything online that relates - there are some that are close but don't quite fit.

This is my script:

#!/bin/bash
# USAGE - runs rake script on redmine@mycompany for email issue reply facility      in redmine
cd /usr/local/src/redmine-3.0.3
rake -f Rakefile redmine:email:receive_imap RAILS_ENV="production"host=imap.gmail.com port=993 ssl=1 username=redmine@mydomain.com password=<my_password> --trace
folder=Inbox
allow_override=true

And the output from the mail when running the job:

Date: Fri, 21 Aug 2015 08:23:01 GMT
Message-Id: <201508210823.t7L8N1U6031959@ip-172-xx-xx-xxx>
X-Authentication-Warning: ip-172-xx-xx-xxx: ec2-user set sender to root using -f
From: root@ip-172-xx-xx-xxx (Cron Daemon)
To: ec2-user@ip-172-xx-xx-xxx
Subject: Cron <ec2-user@ip-172-xx-xx-xxx> sh /usr/local/bin/redmine-email.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/ec2-user>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=ec2-user>
X-Cron-Env: <USER=ec2-user>

/usr/local/bin/redmine-email.sh: line 5: rake: command not found

My crontab:

*/1 * * * *  sh /usr/local/bin/redmine-email.sh

So I have no idea what is going on - I run other cron jobs on this machine with no issues at all. Would appreciate any help, thanks.


Solution

  • The issue here was that cron didn't have the PATH information. Added this and the job runs without an issue. The following information was entered into crontab and voila.

    PATH="/home/ec2-user/.rvm/gems/ruby-1.9.3-p551/bin:/home/ec2-user/.rvm/gems/ruby-1.9.3-p551@global/bin:/home/ec2-user/.rvm/rubies/ruby-1.9.3-p551/bin:/home/ec2-user/.rvm/gems/ruby-1.9.3-p551/bin:/home/ec2-user/.rvm/gems/ruby-1.9.3-p551@global/bin:/home/ec2-user/.rvm/rubies/ruby-1.9.3-p551/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.rvm/bin:/home/ec2-user/bin"
    GEM_HOME='/home/ec2-user/.rvm/gems/ruby-1.9.3-p551'
    GEM_PATH='/home/ec2-user/.rvm/gems/ruby-1.9.3-p551:/home/ec2-user/.rvm/gems/ruby-1.9.3-p551@global'
    MY_RUBY_HOME='/home/ec2-user/.rvm/rubies/ruby-1.9.3-p551'
    IRBRC='/home/ec2-user/.rvm/rubies/ruby-1.9.3-p551/.irbrc'
    RUBY_VERSION='ruby-1.9.3-p551'
    

    Thanks for your help everyone, apologies if my terminology is no good but I am still learning.