2

I want to log the full name of the script executed by the user including the options/arguments he provided to that script.

I was able to achieve some success using basename and whoami but it does not give me the arguments.

Example: let's assume user ran script like this

root@host1:./collect_user_summary.ksh -h 20 -l 10 -x local

Desired Output in my log file:

"collect_user_summary.ksh -h 20 -l 10 -x local"

What I have managed to achieve: with basename and whoami I was able to get

"collect_user_summary.ksh  username"

My environment: Solaris 10, kornshell.

terdon
  • 242,166
ayrton_senna
  • 1,091

1 Answers1

0

Update: I see most of you pointing me to logging of all commands entered in terminal. I dont want that. All i want to do is whenever someone execute my script i just need to log what options he gave along with script name.

After thinking for a while i was able to figure it out. I am using the following method to achieve it. Sample Script:

#!/bin/ksh
NAME=`basename $0`
echo `date` $WHOAMI ran this script $NAME with options -h $HVARIABLE -l $LVARIABLE -x $LOCALVARIABLE >> scripts.log
.
..
..
....
echo "SUCCESS" >> scripts.log
exit 0

By including these 2 lines in all my scripts i was able to log who is doing what and what options they are providing.

ayrton_senna
  • 1,091
  • permissions/ownership may be a problem if multiple users write to the same log file. pre-create the log file with either 660, 664 or 666 perms. 2. try echo $(date) $WHOAMI ran this script $NAME with options: "$@" >>> scripts.log - "$@" lists all command-line options. and use $() rather than backticks...backticks are deprecated and mess up quoting.
  • – cas Dec 02 '15 at 23:16
  • Permissions is not a problem for my log file, since only users with root previlage can execute our scripts. I have't tried $@ in KSH. I find backticks workng lot better than brackets in my solaris & KSH combination. – ayrton_senna Dec 02 '15 at 23:30
  • "$@" works in ksh. 2. You can't easily nest backticks and they mess up quoting. See http://stackoverflow.com/questions/9449778/what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts
  • – cas Dec 02 '15 at 23:39
  • @cas I get your point. I never got a situation to nest multiple variables into a single variable, backticks and pipes were sufficient. Thanks for your tip. It'll be useful someday down the road. – ayrton_senna Dec 02 '15 at 23:43