Debugging Non-Interactive Bash Scripts – Pipe Everything to Syslog

Here’s a cool hack I’ve recently come across when I wanted to modify a Bash script that runs in the background without a shell:

As usual when I touch Bash with its strange syntax, my modifications didn’t work at first, and only sending debug messages with logger() to syslog wasn’t doing me much good in this case. So I was thinking that it would nice if I could hook into stdout and stderr of the script to see Bash’s error message when the script falls over? So I googled a bit and found a solution that pipes all output to syslog. All that is required is adding the following line at the beginning of the script:

exec 1> >(logger -s -t $(basename $0)) 2>&1

Looks short and innocent and very Bash syntax like. And it works like a charm, all bash script output goes to the syslog. And if you would like to understand just what this line does, have a look here.