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

前端 未结 1 940
独厮守ぢ
独厮守ぢ 2021-02-06 12:08

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

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

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


        
相关标签:
1条回答
  • 2021-02-06 12:22

    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  
    
    0 讨论(0)
提交回复
热议问题