Skip to content
/ onerror Public

Run a command and display its output only if the command exits on error

License

Notifications You must be signed in to change notification settings

rsubr/onerror

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OnError

Description

This application runs a command and captures its stdout and stderr. If the command exits successfully, the command output is sent to syslog. If the command fails, then the output is sent to stderr and syslog.

Building

git clone https://github.com/rsubr/onerror.git
cd onerror
go build
strip onerror

Usage

Use this application in crontab where you want to be emailed only if the cron job fails.

# Sample /etc/crontab
# Cron sends email every hour irrespective of borg backup success
# or failure
@hourly root /usr/local/sbin/borg-backup-foo.sh


# Cron sends email only if the backup script failed 
@hourly root onerror /usr/local/sbin/borg-backup-bar.sh

When calling bash scripts from onerror, you must use set -euo pipefail to ensure the script fails and exits immediately whenever any command fails.

#!/bin/bash
# Sample bash script

# do not continue script on errors
set -euo pipefail

pg_dump ...
borg create $REPO::foo-{now} $MY_FILES

About

Run a command and display its output only if the command exits on error

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages