Exit gracefully when piped stdout becomes invalid

This should fix #23.
Tested successfully on Arch Linux using systemd socket activation.