Search code examples
bashsyslog

how can i capture every "set -x" log to syslog?


The scenario is that

I have a shell script that run a few complicated command with different parameters and options.

Those parameters and options are passed by third party applications, such that I may not know exactly what parameters and options are passed each time to the shell script

So in order to know that, i use set -x at the start of the shell script to see what exactly the command is running. And i wanna move those debugger message from set -x to the syslog using logger.

i use trap to do

trap "logger $(BASH_COMMAND)" DEBUG

However the output of the syslog does not have the exact command it is running but sth like that..

apple=1
orange=2
command $apple $orange

.............................

but i wanna the output of the log should be

command 1 2 

how can i achieve?


Solution

  • Here is a way:

    #!/bin/bash
    
    apple=1
    orange=2
    set -x
    
    exec 2> >(logger)
    echo $apple $orange
    

    We are redirecting stderr (set -x routes to stderr, file descriptor 2) to the logger program.

    In my /var/log/messages I get:

    Aug 23 08:16:07 yogi logger: + echo 1 2