Search code examples
unixdatetimekshsudosu

"who am I" after "sudo su -i" always displays the real user's login datetime in ksh


I need to record the real user and the real time into the history list. For that I use trap 'who am i|read -s' debug, but it does not record the real time. Here are the steps so you can verify this.

  1. Login to my personal account on LINUX, say as byksk0, at 2015-10-08 10:38;
  2. Run sudo su - wsadmin
  3. Run who am i, it returns:

    byksk0   pts/0        2015-10-08 10:38 (255.255.255.0)
    
  4. Rerun who am i, it ALWAYS returns:

    byksk0   pts/0        2015-10-08 10:38 (255.255.255.0)
    

    regardless how long this is run after the sudo su - command.

Is it possible to get the real time when a command is executed under wsadmin?


Solution

  • The "who" command returns the login time, not the time of execution of a last command or anything like it. To get a timestamp: use the "date" command.

    EDIT:

    If you want that on one line:

    who am i | awk '{printf "%s ", $1}' && date

    EDIT:

    To try to anticipate the next question: Date also supports formatting of the date and time it outputs e.g:

    $ who am i | awk '{printf "%s ", $1}' && date "+%Y-%m-%d %H:%M:%S" swa66 2015-10-08 21:20:34