3

In my .bash_profile I have script -a -t 0 session.log which automatically records all terminal input/output into the session.log file. I know this command comes with an optional flag to record the time differences in another file but I was wondering if there was another way to prefix the session.log lines with a timestamp.

Can I pipe the writes to a different shell function before writing it to session.log? Something like this script -a >(add_timestamps.sh >> session.log) (does not work)

  • Is there anything special in the format about these script files, or could this question be reworded to be about prepending timestamps to files in general? – Anko Apr 21 '16 at 0:38
  • ideally I would like the timestamp to be as automated and simple as possible so i'm trying to avoid tailing the file and then writing to a new one. – Luiz Apr 21 '16 at 2:10
2

There is probably only one thing missing from your example, the option -f to flush the write each time.

script -f >(while read;do date;echo "$REPLY";done >>session.log)
| improve this answer | |
  • Wow you're right... I was closer than I realized.. for osx however you have to do script -t 0 to flush each time. I also ended up going with this function to add the date... serverfault.com/a/310104 – Luiz Apr 21 '16 at 21:49
1

this might be a partial solution, depending on your needs: if you include \d \D{} in your PS1 string, each command prompt will include the date and time. that will give you the time at which the previous command finished. in the simplest case, do

    PS1='\d \D{} $ '

do that after invoking script (or in your .bashrc or whatever) and you will get a session.log like this:

    Wed Apr 20 08:23:55 PM $ date
    Wed Apr 20 20:23:57 EDT 2016
    Wed Apr 20 08:23:57 PM $ exit
| improve this answer | |
  • This is pretty slick but I don't want to pollute the Prompt. – Luiz Apr 21 '16 at 2:09
0

Sounds like ts, from moreutils.

Lots of distros have it packaged. It just prepends timestamps to lines of input.

Example use:

$ while true; do sleep 1; echo hi; done | ts %s
1461198715 hi
1461198716 hi
1461198717 hi
1461198718 hi
[ ... ]
| improve this answer | |
  • The code needs to be portable and not just for local use so I can't assume everyone will have it installed – Luiz Apr 21 '16 at 2:09
0

By using awk as the log output destination, you can add a timestamp to the beginning of each line.

script -fq >(awk '{print strftime("%F %T ") $0}{fflush() }'>> session.log)

Example)

[root@BS-PUB-CENT7-01 ~]# LANG=C script -fq >(awk '{print strftime("%F %T ") $0}{fflush() }'>>
session.log)
[root@BS-PUB-CENT7-01 ~]# pwd
/root
[root@BS-PUB-CENT7-01 ~]#
[root@BS-PUB-CENT7-01 ~]# echo aaa
aaa
[root@BS-PUB-CENT7-01 ~]#
[root@BS-PUB-CENT7-01 ~]# exit
exit
[root@BS-PUB-CENT7-01 ~]#
[root@BS-PUB-CENT7-01 ~]# cat session.log
2017-05-26 01:12:11 Script started on Fri May 26 01:12:11 2017
2017-05-26 01:12:13 [root@BS-PUB-CENT7-01 ~]# pwd
2017-05-26 01:12:13 /root
2017-05-26 01:12:14 [root@BS-PUB-CENT7-01 ~]#
2017-05-26 01:12:16 [root@BS-PUB-CENT7-01 ~]# echo aaa
2017-05-26 01:12:16 aaa
2017-05-26 01:12:16 [root@BS-PUB-CENT7-01 ~]#
2017-05-26 01:12:17 [root@BS-PUB-CENT7-01 ~]# exit
2017-05-26 01:12:17 exit
| improve this answer | |
  • 1
    Please add some explanation if possible. – phk May 23 '17 at 19:06
  • I'm sorry, I was not good at English... – Naofumi Uesugi May 26 '17 at 1:15

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.