A shell script can lose its standard and other file descriptors if the other end goes away. One way this can happen is by terminating a ssh client used to launch the script:$ ssh localhost test^CThis launches a bash script called test over SSH and a CtrlC is used to terminate the ssh command. This does not terminate test which continues running but it does close the attached file descriptors (e.g. standard input/output/error).If test tries to use those closed file descriptors then it will terminate with a SIGPIPE. A simple echo will suffice.One of the things test might have is a log function that uses echo to write messages to the systemd journal and also to standard error. Something like this:log() { echo $* | systemd-cat -t test echo >&2 $*}The second echo raises a SIGPIPE after the ssh session is killed (CtrlC). This can be protected against, so that execution continues without the offending echo, by putting the echo in a subshell:( echo >&2 $* )But is there a better way?
How can a shell script avoid the SIGPIPE that would be caused by use of closed file descriptor?
bash;shell script;shell