Search code examples
ruby-on-railsrubycronwhenever

Can't get Ruby Whenever working


I'm having some trouble getting whenever to work in my local dev environment. I'm using mac OS 10.12, ruby 2.4, rails 5.1 and whenever 0.10.0.

I have my schedule.rb file setup as:

set :environment, "development"
set :job_template, "TZ=\"America/Los_Angeles\" bash -l -c ':job'"
set :output, Whenever.path + "/log/cron.log"

every 5.minutes do
  runner "Test.new.run"
  command "/bin/echo 'Is this working?'"
end

crontab -l outputs the following:

# Begin Whenever generated tasks for: /Users/me/apps/Test/config/schedule.rb at: 2018-05-25 12:59:14 -0700
0,5,10,15,20,25,30,35,40,45,50,55 * * * * TZ="America/Los_Angeles" bash -l -c 'cd /Users/me/apps/Test && bundle exec bin/rails runner -e development '\''Test.new.run'\'' >> /Users/me/apps/Test/log/cron.log 2>&1'

0,5,10,15,20,25,30,35,40,45,50,55 * * * * TZ="America/Los_Angeles" bash -l -c '/bin/echo '\''Is this working?'\'' >> /Users/me/apps/Test/log/cron.log 2>&1'


# End Whenever generated tasks for: /Users/me/apps/Test/config/schedule.rb at: 2018-05-25 12:59:14 -0700

and I'm receiving the following when I run the mail command in terminal:

  Message 1:
From [email protected]  Fri May 25 13:00:01 2018
X-Original-To: me
Delivered-To: [email protected]
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <me@box> TZ="America/Los_Angeles" bash -l -c '/bin/echo '\''Is this working?'\'' >> /Users/me/apps/Test/log/cron.log 2>&1'
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=me>
X-Cron-Env: <USER=me>
X-Cron-Env: <HOME=/Users/me>
Date: Fri, 25 May 2018 13:00:00 -0700 (PDT)

However, I'm not seeing anything output to cron.log nor does the DB reflect any activity from Test.new.run

Not really sure what I'm doing wrong...


Solution

  • I think the problem may have had to do with rvm and the .rvmrc file I had set up which loaded the correct rvm settings when I cd into the directory.

    In attempting to ensure that the command was working opened a new terminal window and ran: bash -l -c 'cd /Users/me/apps/Test && bundle exec bin/rails runner -e development '\''Test.new.run'\'' >> /Users/me/apps/Test/log/cron.log 2>&1'

    The first thing that came up was the standard 1 off rvm message:

    You are using '.rvmrc', it requires trusting, it is slower and it is not compatible with other ruby managers,
    you can switch to '.ruby-version' using 'rvm rvmrc to ruby-version'
    or ignore this warning with 'rvm rvmrc warning ignore /Users/me/apps/Test/.rvmrc',
    '.rvmrc' will continue to be the default project file in RVM 1 and RVM 2,
    to ignore the warning for all files run 'rvm rvmrc warning ignore all.rvmrcs'.
    
    ********************************************************************************
    * NOTICE                                                                       *
    ********************************************************************************
    * RVM has encountered a new or modified .rvmrc file in the current directory,  *
    * this is a shell script and therefore may contain any shell commands.         *
    *                                                                              *
    * Examine the contents of this file carefully to be sure the contents are      *
    * safe before trusting it!                                                     *
    * Do you wish to trust                                                         *
    * '/Users/me/apps/Test/.rvmrc'?                                                *
    * Choose v[iew] below to view the contents                                     *
    ********************************************************************************
    y[es], n[o], v[iew], c[ancel]> 
    

    Once I typed y and hit enter, the cron jobs are now executing properly.

    Unfortunately, I'm still not seeing anything in my cron.log file.