I'm setting up an email notification system where I want to save failed commands (edit: failed scripts) in a .txt file and once a day I have a python script to send out an email containing all the failed jobs.
It currently looks like either: script1.py && script2.py && ... && scriptN.py 2>> "/home/mydir/failedscripts.txt" or script1.sh && script2.sh arg1 && .. && scriptN.sh 2>> "/home/mydir/failedscripts.txt"
Some scripts takes arguments. It seems like it works fine with the redirecting of stderr, but I need to pipe the name of the failed script + the string "DONE" into "failedscripts.txt" as well.
My failed tries:
script1 && script2 && .. && scriptN 2>> "/home/mydir/failedscripts.txt" || echo $_,"done">> "/home/mydir/failedscripts.txt"
script1 && script2 && .. && scriptN 2>> "/home/mydir/failedscripts.txt" ; echo $_,"done">> "/home/mydir/failedscripts.txt"
script1 && script2 && .. && scriptN 2>> "/home/mydir/failedscripts.txt" && echo $_,"done">> "/home/mydir/failedscripts.txt"
But this surely just pipes "failedscripts.txt,done" to my failedscripts.txt....
Here's a way which doesn't give exactly the output format you wish, but does indicate the failed script by writing every executed script to the file, so that the last one before DONE is the failed one:
(set -x; script1 && script2 && … && scriptN) 2>/tmp/$$ || (cat /tmp/$$; echo DONE) >>/home/mydir/failedscripts.txt; rm /tmp/$$