Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whenever gem: I set :output but the logfile doesn't show up where I'd expect it to

In my schedule.rb file I have the following lines:

set :output, '/log/cron_log.log'

every 5.minutes do
  command 'echo "hello"'
end

I ran whenever -w as suggested in this question Rails, using whenever gem in development, and I assume the cronfile is written and running. (I tried restarting the Rails server as well.)

And when I run $ crontab -l I see the following:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash
-l -c 'echo "hello" >> /log/cron_log.log 2>&1'

But I can't find the log file. I checked in rails_project/log, ~/log and /log to no avail. On OSX btw.

How can I get the log file to be written to the rails project log folder?

like image 796
AJcodez Avatar asked Jan 30 '13 20:01

AJcodez


1 Answers

Where is your log?

You're putting the output file at the highest directory level:

$ cd /log

To see if the file exists and if it has data in it:

$ ls -la cron_log.log

To create the log file if needed:

$ touch cron_log.log

To open up permissions for your own local debugging (do NOT do this in production!)

$ chmod +rw cron_log.log  

Is your command running?

To run the command manually to find out if it works as you expect:

$ /bin/bash -l -c 'echo "hello" >> /log/cron_log.log 2>&1'

To improve your security and protect your path, use full paths:

wrong: command 'echo "hello"'
right: command '/bin/echo "hello"'

To find the command full path:

$ which echo

To verify the cron is running as you expect:

$ sudo grep CRON /var/log/syslog

The grep result should have lines that something like this:

Jan 1 12:00:00 example.com CRON[123]: (root) CMD (... your command here ...)

Are you on a Mac?

If you're not seeing output in the syslog, and you're on a Mac, you may want to read about the Mac OSX switching from cron to launchd.

See the cron plist (/System/Library/LaunchDaemons/com.vix.cron.plist) and use a stdout/stderr path to debug cron itself. I don't recall if launchctl unloading and launchctl loading the plist is sufficient, or since it's a system daemon if you'd have to restart entirely. (see where is the cron log file in lion)

How to log relative to Rails?

To put the log relative to a Rails app, omit the leading slash (and typically call it cron.log)

set :output, "log/cron.log"

To put the log in a specific fully-qualified directory:

set :output, '/abc/def/ghi/log/cron.log'

The Whenever wiki has some good examples about redirecting output:

https://github.com/javan/whenever/wiki/Output-redirection-aka-logging-your-cron-jobs

Examples:

every 3.hours do
  runner "MyModel.some_process", :output => 'cron.log'     
  rake "my:rake:task", :output => {:error => 'error.log', :standard => 'cron.log'}
  command "/usr/bin/cmd"
end  
like image 191
joelparkerhenderson Avatar answered Oct 06 '22 19:10

joelparkerhenderson