Monthly Archives: March 2006

Logging commands in korn shell

Yet another blast from the past, but I was asked for this again today.

How can you log every command typed into a korn shell session? Here is the cheap and dirty but surprisingly useful way that logs them all into syslog.

Type this into your shell and you can capture the command, it’s return code and the current working directory.

function dlog
{
typeset -i stat=$?
typeset x
x=$(fc -ln -0)
logger -p daemon.notice -t “ksh $LOGNAME $$” Status $stat PWD $PWD ’${x# }’
}
trap dlog DEBUG

(note that there is a tab after the # in “${x# }”)

You might want to use a different logging facility but that one gets it into /var/adm/messages:

Mar  2 14:44:15 estale ksh cg13442 497922: [ID 702911 daemon.notice] Status 0 PWD /home/cg13442 ‘ls’
Mar 2 14:44:18 estale ksh cg13442 497922: [ID 702911 daemon.notice] Status 1 PWD /home/cg13442 ‘false’
Mar 2 14:45:09 estale ksh cg13442 497922: [ID 702911 daemon.notice] Status 0 PWD /home/cg13442 ‘ls -la’

I had run ls, false and “ls -la” which is dutifully logged.

Tags: topic:[solaris] topic:[korn shell]

WP Like Button Plugin by Free WordPress Templates