kdave / btrfsmaintenance

Scripts for btrfs maintenance tasks like periodic scrub, balance, trim or defrag on selected mountpoints or directories.
GNU General Public License v2.0
897 stars 79 forks source link

Error handling #72

Open macropin opened 5 years ago

macropin commented 5 years ago

These scripts don't handle errors. Please consider set -e at the top of each script so that error conditions will be trapped. The exit 0 at the bottom of each script can also be removed as it's redundant.

Use case / justification:

We monitor btrfs-balance.service, however when an error condition is encountered the script returns with an exit code of 0 even though the balance failed: ERROR: error during balancing '/backup': No space left on device There may be more info in syslog - try dmesg | tail

kdave commented 4 years ago

The exit 0 overriedes the last exit code, which is call to the logging facility (systemd-cat, cat, logger), so I'm not sure this is what your monitoring is interested in. Catching the error code of the previous comand group (calling btrfs) can be extracted from PIPESTATUS though.

Adding set -e makes sense, though not all errors are fatal. The balance tries to make the space more compact but may not succeed. But this is the message you posted as an example so I guess you want to see that anyway.