Piping the "script" command through the logger command.

I use the snippet below in /etc/profile on RHEL Linux to capture command line logging and it all works well and good.

Now I'd like to pipe the same output from script through the logger command so it all gets logged to syslog.

The only additional code I've added is in bold below (| bin/logger).

This works as expected sans one issue, albeit a major one. My terminal session is blank as if nothing is being typed, however if I type commands I can see them being logged and if I type exit my session closes. I tried a nohup and & to see if that would help but it does not. I'm wondering why I can no longer see anything on my tty.

This is what my putty session looks like. So I have a fully functional session but I can't see any output.
[user@test1 ~]$ ssh cxxx
user@test1's password:
Last login: Thu Sep 12 09:56:01 2013 from 10.x.x.x

if [ -z $PS1 ]
  then
    echo "" > /dev/null
  else
    DATE="/bin/date"  SCRIPT="/usr/bin/script"
    LOGBASE="/log/cmdline_logs"
       if [ -d "${LOGBASE}" ]; then
           TIMESTAMP="$( ${DATE} +%Y%m%d%H%M%S )"
           LOGFILE="${LOGBASE}/${HOSTNAME}_${USER}_${TIMESTAMP}"
           umask 077
           [[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_profile" ]] && . ${HOME}/.bash_profile
           ${SCRIPT} -f -q ${LOGFILE}.log | /bin/logger
           [[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_logout" ]] && . ${HOME}/.bash_logout
           exit
       fi
  
fi

This is a tricky one, because each chain of pipes creates more possibilities for buffering and stalling.

Since you're creating a file anyway, why not tail it, to feed it into logger instead of putting a pipe in the way?

: > $LOGFILE.log
( tail -f $LOGFILE.log | /bin/logger ) &

trap "kill $!" EXIT # Kill the logger subshell on exit

${SCRIPT} -a -f -q ${LOGFILE}.log

I'm not sure that feeding raw, binary keystrokes into logger is a terrific idea, though. A lot of what script saves, only looks nice when dumped to a shell.

Also, instead of echo "" > /dev/null as a do-nothing stub, you can use the easier and simpler :

Thanks,

Can you show me how this code fits into what I have already?

I'm not sure it's 100% perfect. You should test it and make sure it's killing the logging processes correctly on logout.

if [ -z $PS1 ]
  then
        :
  else
    DATE="/bin/date"  SCRIPT="/usr/bin/script"
    LOGBASE="/log/cmdline_logs"
       if [ -d "${LOGBASE}" ]; then
           TIMESTAMP="$( ${DATE} +%Y%m%d%H%M%S )"
           LOGFILE="${LOGBASE}/${HOSTNAME}_${USER}_${TIMESTAMP}"
           umask 077
           [[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_profile" ]] && . ${HOME}/.bash_profile

                : > $LOGFILE.log
                ( tail -f $LOGFILE.log | /bin/logger ) &

                trap "kill $!" EXIT # Kill the logger subshell on exit

                ${SCRIPT} -a -f -q ${LOGFILE}.log

           [[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_logout" ]] && . ${HOME}/.bash_logout
           exit
       fi
fi
1 Like

This seems to work very well. Thank you.