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

1

7

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?

Plassey answered 30/1, 2013 at 20:14 Comment(1)
this works for me. without leading slash '/' . set :output, 'log/cron_log.log'Anfractuosity
A
37

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  
Aphorism answered 30/1, 2013 at 21:27 Comment(4)
getting uninitialized constant Whenever::JobList::Rails (NameError). Do you know how do I get Rails recognized?Plassey
thank you so much for your patience. tried that as well and it's still not showing up. Is it possible the cron tasks aren't running? Or is my echo command not doing what I think it is? I wanted to see the log to know whether or not it's even running..Plassey
echo is fine, but what should i get from the $ sudo grep CRON /var/log/system.log? Cause i dont think im getting anythingPlassey
I added sample grep output and Mac info to the answer.Aphorism

© 2022 - 2024 — McMap. All rights reserved.