3

Is it possible to have your bash history save the total execution time of every command you run?

i.e. as if every command were prefaced with 'time' and the result was stored in the history along with the command?

clarification: I already know that you can add a timestamp of when the command was started - but I would like to know the total execution time, not just when it was started.

  • not a duplicate - see clarification – slashdottir Dec 5 '16 at 15:27
  • 1
    Ah, OK, I'd understood "actual execution time" as "time at which the command was executed". – Stephen Kitt Dec 5 '16 at 15:30
  • Possible duplicate of Bash history with timestamps – techraf Dec 5 '16 at 20:08
  • 1
    @tecraf - not a duplicate - see clarification – slashdottir Dec 5 '16 at 20:19
1

By defining HISTTIMEFORMAT in your startup scripts, you will have a timestamp added to every line of your bash history file. The value of this variable is the way (*) timestamps must be printed on the screen. Here is mine :

HISTTIMEFORMAT='%F-%T '

(*) See man 3 strftime (if you don't have it, man date should do the trick)

  • 1
    yes, that will tell you what time you ran the command - but not the total execution time of it. – slashdottir Dec 5 '16 at 15:26
  • 1
    @slashdottir Sorry, I must have misread your question. I don't know of any such feature. – xhienne Dec 5 '16 at 15:29
1

Child process usage is set somewhere within the wait3 or wait4 system calls via a struct rusage pointer (wait4(2)). Under the bash sources, we find:

$ egrep -r 'wait[34][[:blank:]]*\(' .
./jobs.c:   wait3 ((union wait *)statusp, options, (struct rusage *)0)
./jobs.c:   wait3 (statusp, options, (struct rusage *)0)
./jobs.c:   wait3 (statusp, options, (int *)0)
$

The blank (struct rusage *)0 indicates that the resource usage will not be returned to bash. A struct rusage search does turn up time_command function in execute_cmd.c, and this does appear to collect usage information via getrusage(2). However, this is for the TIMEFORMAT of the time builtin, and does not appear to intersect with the history code. Without patching bash, I guess you'd have to do some sort of kluge involving running everything under time and then capturing the standard error the time information appears on to some file?

  • I'm not above a good kluge ... :) – slashdottir Dec 5 '16 at 17:51

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.