Not able to test cron job using whenever gem in rails 3

此生再无相见时 提交于 2019-12-25 02:51:34

问题


I did the following to implement cron jobs in rails 3 using a "runner" instead of a rake task.

Step 1: I have whenever gem installed and scheduler.rb has following:

  set :environment, 'development'
  set :output, {
 :error    => "/log/error.log",
 :standard => "/log/cron.log" 
  }

  every 1.minute do
  runner "Cron.sendAutomaticsSMS()"
  end

Step 2:

Cron file: lib/cron.rb

    class Cron < ActiveRecord::Base

      def **sendAutomaticsSMS**()
         ----some code here ---
      end
    end

Step 3:

    whenever --update-crontab --set environment=development 

This updates crontab file.

Step 4:

Checked contents of crontab file using:

    crontab -e  

It opens cron tab file which has details of cron job method to be invoked:


    # Begin Whenever generated tasks for: store

    # End Whenever generated tasks for: store

    # Begin Whenever generated tasks for: /code/rails_projects/new/bhk/bigbhk-     dev/config/schedule.rb
    * * * * * /bin/bash -l -c 'cd <*RAILS_APP_PROJECT_LOCATION*> && script/rails runner -e development '\''**Cron.sendAutomaticsSMS()**'\'' >$

    # End Whenever generated tasks for: /code/rails_projects/new/bhk/bigbhk-dev/config/schedule.rb

Step 5:

Running cron job:

    $sudo service cron restart

    cron stop/waiting
    cron start/running, process 4027

This does not do anything. I wait for operation as defined in sendAutomaticsSMS() method to execute but the cron doesn't even enter this method.

I don't see any error in log/development.log and there is no log/cron.log in my rails app.


回答1:


I checked the contents of mail drop files as suggested by tamouse. I see a cron log location referred to in these files but these files are no where generated or present in the respective location.

   /var/spool/cron/crontabs/<username>

Begin Whenever generated tasks for: <RAILS_ROOT>/config/schedule.rb /bin/bash -l -c 'cd <RAILS_ROOT> && script/rails runner -e development '\''Cron.sendAutomaticsSMS()'\'' >> /log/cron.log 2>> /log/error.log'

   /var/mail/<username>

From ankur@ankur-Lenovo-G470  Fri May 30 21:29:01 2014
Return-Path: <ankur@ankur-Lenovo-G470>
X-Original-To: ankur
Delivered-To: ankur@ankur-Lenovo-G470
Received: by ankur-Lenovo-G470 (Postfix, from userid 1000)
        id D885D16172C; Fri, 30 May 2014 21:29:01 +0530 (IST)
From: root@ankur-Lenovo-G470 (Cron Daemon)
To: ankur@ankur-Lenovo-G470
Subject: Cron <ankur@ankur-Lenovo-G470> /bin/bash -l -c 'cd <RAILS_ROOT> && script/rails runner -e development '\''Cron.sendAutomaticsSMS()'\'' >> /log/cron.log 2>> /log/error.log'
Content-Type: text/plain; charset=ANSI_X3.4-1968

X-Cron-Env: < SHELL=/bin/sh > 

X-Cron-Env: < HOME=/home/username >

X-Cron-Env: < PATH=/usr/bin:/bin >

X-Cron-Env: < LOGNAME=username >

Message-Id: <20140530155901.D885D16172C@ankur-Lenovo-G470>
Date: Fri, 30 May 2014 21:29:01 +0530 (IST)


/bin/bash: /log/cron.log: No such file or directory



回答2:


Your cron task is trying to write to /log/, i.e. a directory at the root level of the file system. Seeing that now in your schedule.rb file:

set :output, {
  :error    => "/log/error.log",
  :standard => "/log/cron.log" 
}

I think what you'll need there is:

log_dir = File.expand_path("../log/", __FILE__)
set :output, {
  :error    => File.join(log_dir, "error.log"),
  :standard => File.join(log_dir, "cron.log") 
}



回答3:


-I find that /var/log/syslog file is updated with cron invocation every 1 minute which is my cron job period. I am listing below log trace for two such consecutive invocations.

-But there is no error file generated in the defined log location and the cron job doesn't execute

   /code/rails_projects/new/bhk/bigbhk-dev/config/log/cron.log (path for this location is defined in config/scheduler.rb file)

-Log Trace:

1st invocation:

    Jun  8 20:02:01 ankur-Lenovo-G470 CRON[27365]: (ankur) CMD (/bin/bash -l -c 'cd /code/rails_projects/new/bhk/bigbhk-dev && script/rails runner -e development '\''Cron.sendAutomaticsSMS()'\'' >> /code/rails_projects/new/bhk/bigbhk-dev/config/log/cron.log 2>> /code/rails_projects/new/bhk/bigbhk-dev/config/log/error.log')
    Jun  8 20:02:02 ankur-Lenovo-G470 postfix/pickup[22599]: 02CEA161719: uid=1000 from=<ankur>
    Jun  8 20:02:02 ankur-Lenovo-G470 postfix/cleanup[22701]: 02CEA161719: message-id=<20140608143202.02CEA161719@ankur-Lenovo-G470>
    Jun  8 20:02:02 ankur-Lenovo-G470 postfix/qmgr[12204]: 02CEA161719: from=<ankur@ankur-Lenovo-G470>, size=878, nrcpt=1 (queue active)
    Jun  8 20:02:02 ankur-Lenovo-G470 postfix/local[22703]: 02CEA161719: to=<ankur@ankur-Lenovo-G470>, orig_to=<ankur>, relay=local, delay=0.14, delays=0.09/0/0/0.04, dsn=2.0.0, status=sent (delivered to mailbox)
    Jun  8 20:02:02 ankur-Lenovo-G470 postfix/qmgr[12204]: 02CEA161719: removed

2nd invocation:

  Jun  8 20:03:01 ankur-Lenovo-G470 CRON[27468]: (ankur) CMD (/bin/bash -l -c 'cd /code/rails_projects/new/bhk/bigbhk-dev && script/rails runner -e development '\''Cron.sendAutomaticsSMS()'\'' >> /code/rails_projects/new/bhk/bigbhk-dev/config/log/cron.log 2>> /code/rails_projects/new/bhk/bigbhk-dev/config/log/error.log')
  Jun  8 20:03:01 ankur-Lenovo-G470 postfix/pickup[22599]: 431E5161719: uid=1000 from=<ankur>
  Jun  8 20:03:01 ankur-Lenovo-G470 postfix/cleanup[22701]: 431E5161719: message-id=<20140608143301.431E5161719@ankur-Lenovo-G470>
  Jun  8 20:03:01 ankur-Lenovo-G470 postfix/qmgr[12204]: 431E5161719: from=<ankur@ankur-Lenovo-G470>, size=878, nrcpt=1 (queue active)
  Jun  8 20:03:01 ankur-Lenovo-G470 postfix/local[22703]: 431E5161719: to=<ankur@ankur-Lenovo-G470>, orig_to=<ankur>, relay=local, delay=0.14, delays=0.09/0/0/0.04, dsn=2.0.0, status=sent (delivered to mailbox)
  Jun  8 20:03:01 ankur-Lenovo-G470 postfix/qmgr[12204]: 431E5161719: removed

-Also in var/log/auth/log, I see following:

  Jun  8 19:52:38 ankur-Lenovo-G470 sudo:    ankur : TTY=pts/3 ; PWD=/code/rails_projects/new/bhk/bigbhk-dev ; USER=root ; COMMAND=/usr/sbin/service cron restart
  Jun  8 19:52:38 ankur-Lenovo-G470 sudo: pam_unix(sudo:session): session opened for user root by ankur(uid=0)
  Jun  8 19:52:38 ankur-Lenovo-G470 sudo: pam_unix(sudo:session): session closed for user root
  Jun  8 20:03:01 ankur-Lenovo-G470 CRON[27467]: pam_unix(cron:session): session opened for user ankur by (uid=0)
  Jun  8 20:03:01 ankur-Lenovo-G470 CRON[27467]: pam_unix(cron:session): session closed for user ankur

-The log messages which I am printing in my cron job method: sendAutomaticsSMS(), they are also not getting logged anywhere which means, the execution of job is not even starting.

Any further debugging clue?




回答4:


I think this is the crux of your issue: /bin/bash: /log/cron.log: No such file or directory. There is no log directory at the root of the drive. Try changing your log paths to /var/log/error.log and /var/log/cron.log.



来源:https://stackoverflow.com/questions/23880580/not-able-to-test-cron-job-using-whenever-gem-in-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!