Tuesday, May 25, 2010

Script to log stepwise execution and output of another bash script into syslog

The input arguments for this script would be another script with its input arguments. First, this script stores the stepwise execution and the output of the called bash script into a temporary file. When the called bash script finishes its execution, this script reads the temporary file and logs them into syslog. We are using a temporary file because if we pipe the output of the called script we will lose the true return value of that script.
#!/bin/bash

tmpfile=$(mktemp)
(/bin/bash -x $@ 2>&1) > $tmpfile
retval=$?
cat $tmpfile | logger -t "logger [$1]"
rm -f $$tmpfile

exit $retval