How would I send an email if a background process I run via nohup fails or is completed? I am running a lot of background processes via nohup in a for loop:

for afolder in $dir/dothis*
    nohup nice COMMAND afolder & 

I guess because of the background process, when I do

nohup nice COMMAND afolder & ; tail nohup.out | mail myemail@here.com -s "job done"

I get an email upon execution, not completion. And how would I send a different email subject line depending on whether the process failed to complete vs. successful completion?

Thanks in advance!

  • Any reason to use nohup instead of at now?
    – mosvy
    Aug 7, 2019 at 11:31

1 Answer 1


One option is to do the mailing in the background job:

for afolder in "$dir"/dothis*; do
    nohup sh -c '

        if nice COMMAND "$1" >"$outfile" 2>&1; then

        tail "$outfile" |
        mail -s "job done ($success_or_fail)" myemail@here.com

        rm "$outfile"' sh "$afolder" &

This does the same loop as you, but then starts a child script running under nohup. This script takes the current value of $afolder as its first command line argument and runs the command in an if statement. It sets success_or_fail appropriately depending on the outcome of the command, and then sends the email.

All output of the command is redirected to a temporary file which is then tail-ed for the email and deleted when the shell exits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.