2

I want to keep a log of all commands I run in a separate log file (other than history) along with other description. To achieve this I need to know how can I fetch current run command and store it in a file.

Scenario: Currently, Whenever I run sudo command it asks for reason, which I provide. And the reason is stored in log file in my directory. But now I want to store the command run along with the reason and I am not able to find the way to do this.

$ sudo touch file
Reason for sudo: creating new file test
[sudo] password for root: 

Like in above, I want to store the command sudo touch file along with reason provided in a file every time I run a command.

  • You replaced "sudo:" with a script to obtain this request to provide a reason? As such, the script could then also read the command line arguments, which contain the command run, and also store that also in the log file. – vanadium Feb 24 at 10:53
  • Thanks vanadium. I used same approach and successfully achieved this. I will be putting that in my answer. – Navi Feb 24 at 15:29
1

You can use a tiny function to override a command of the same name, add your extensions, and then run the actual command e.g. for sudo (assuming bash):

sudo () {
    read -p 'Reason for sudo: ' reason
    printf 'sudo %s : %s\n' "$*" "$reason" >>~/reason_file.txt
    command sudo "$@"
}

Put that in any shell session initialization file e.g. ~/.bashrc. And obviously you can modify the function body to meet your need.

Now when you'll be runningsudo some_command, the above function will be run and will do the defined operations before eventually running sudo some_command with the actual sudo external binary.


As a side note, all shells store the interactive session history in a separate file e.g. for bash it”s ~/.bash_history, for zsh it’s ~/.histfile. And the history builtin command will get you the saved (and buffered) history.

0

Thanks for suggestions. I achieved this using an alias in my .bashrc and a shell script.

alias sudo='bash $HOME/.sudolog'

Script $HOME/.sudolog

#!/bin/bash

#Script to log the sudo commands used with reason.

##                                                                       ##
#LAST UPDATED ON 24 FEB 2019 16:30 IST#
##                                                                       ##

logfile="$HOME/sudo.log"
cmd="sudo $*"

date_time=`date +'%d/%m/%Y %H:%M:%S'` > /dev/null 2>&1
username=`whoami` > /dev/null 2>&1
read -p "Reason for sudo: " reason
eval $cmd
rtrn="$?"
echo "<date time> $date_time </date time>  <user> $username </user>  <command> sudo $* </command>  <reason> $reason </reason>  <return> $rtrn </return>"  >> "$logfile"
  • Are you sure using cmd="sudo $*" and eval $cmd rather than cmd="sudo $@" and eval "$cmd" is a good idea? Try with sudo touch "a file", this should create just one file. – dessert Feb 24 at 15:52
  • A bash shortcut for > /dev/null 2>&1 is &>/dev/null, though I’m not sure what you’re redirecting in these lines… – dessert Feb 24 at 15:55
  • $* worked for me well. So haven't checked with $@. Do let me know if it will create any issue in future – Navi Feb 24 at 15:55
  • /dev/null 2>&1 is to redirect any error or output to null so that it won't create clutter on terminal – Navi Feb 24 at 15:57
  • “If you want to re-use your positional parameters to call another program (for example in a wrapper-script), then this is the choice for you, use double quoted "$@".” (source) – dessert Feb 24 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.