Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active May 11, 2017 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/7e56d9f6e359c288005ccb037a935df1 to your computer and use it in GitHub Desktop.
Save trepmal/7e56d9f6e359c288005ccb037a935df1 to your computer and use it in GitHub Desktop.
<some-command> | tee some.log ; \
  ( [[ ${PIPESTATUS[0]} -eq 1 ]] \
  && (tail some.log | mail -s '<some-command> exited with error' you@example.com) \
  || (tail some.log | mail -s '<some-command> finished' you@example.com) )

breakdown:

<some-command> | tee some.log;
run command, tee output to log (display in terminal, and writes to file)

(
group this stuff

[[ ${PIPESTATUS[0]} -eq 1 ]]
check exit code of <some-command>. this is bash specific.

&& (tail some.log | mail -s '<some-command> exited with error' you@example.com)
on success, email tail of log

|| (tail some.log | mail -s '<some-command> finished' you@example.com)
else, on fail also email tail of log

)
close

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment