Error Monitoring for Cron Jobs and Command-Line Programs

Version 3.x of the Honeybadger gem sports a lovely new CLI interface that can take the STDERR of any cron job or command-line program and report it to Honeybadger as if it were an error in your Rails app.

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

If 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

The new 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.

By default, 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"'

Configuration

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.

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like: