Error Monitoring for Cron Jobs and Command-Line Programs
Most web apps have some pieces that live outside of the application itself. Backups have to run, datasets have to be imported and dunning emails have to be sent.
But what happens if there's an error?
When you're working inside the Rails environment like many Rake, Sidekiq and Resque jobs do, errors are reported by Honeybadger automatically. But if your task doesn't load the Rails environment it's been difficult to report failures to Honeybadger...until now.
Version 3.x of the Honeybadger gem sports a lovely new CLI interface that can take the STDERR of any program and report it to Honeybadger as if it were an error in your Rails app.
Here's what it looks like:
$ honeybadger exec my-command --my-flag
my-command writes to STDERR or exits with a non-zero status code (indicating an error) then Honeybadger will send the command's STDERR and STDOUT output to our servers and alert you via email, slack or any other of our third-party integrations.
Built for Cron
honeybadger exec feature was built with cron in mind. If you've ever run cron on a production server you know that its notification system leaves a lot to be desired. It will email you whenever your job outputs anything whether there was an error or not.
honeybadger exec will only write to STDOUT if there is an error. That means that cron will only email you if there's an error. This can be useful if you want a redundant error report in addition to the one that Honeybadger will send you.
To see this in action, we can run a command that does nothing but write a test string to STDERR:
honeybadger exec '>&2 echo "error"' Honeybadger detected failure or error output for the command: `>&2 echo "error"` PROCESS ID: 85296 RESULT CODE: 0 ERROR OUTPUT: error Successfully notified Honeybadger
If you'd like to prevent cron from emailing you at all, you can suppress all output via the
--quiet flag. Of course, the error will still be sent to Honeybadger, and we'll still notify via email, slack or another channel:
honeybadger exec --quiet '>&2 echo "error"'
If you run
honeybadger exec in the root directory of your Rails project, it will automatically use the Honeybadger configuration from that project.
If you'd like to use
honeybadger exec outside of your Rails environment, you'll need to provide its API key and other configuration options.
You can set the API key and the environment name by using command-line arguments:
honeybadger exec --api-key=12345 --env=production my-command --my-flag
For more advanced configuration of
honeybadger exec you can put configuration options in
~/honeybadger.yml or in your environment variables. See the Docs.