Finding command execution time in hindsight

I’ve just executed a long-running process from the bash prompt. In hindsight, I wish I’d run time on it, or noted down the time at which I kicked it off.

Is there any way of getting this information retrospectively? The .bash_history doesn’t seem to include timestamps.

In my particular case it’s Mac OS X, but I’m interested in general Unix/Linux solutions.

To clarify, the process has now completed, and I’d prefer not to run it again unless absolutely necessary!


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

bash actually remembers the times until you close the shell.

So try running

HISTTIMEFORMAT='%x %X ' history

If you also put
HISTTIMEFORMAT=<some format>

in your ~/.bashrc, it will also get written to ~/.bash_history on exit, so you can check what happened in previous shell sessions too.

Method 2

Mikel’s answer is good, except that if you run the program and go away for a while, you can’t really be sure when the process finished. So even if you have the time when you started the program, you don’t know how much time it took.

I don’t have a solution for the case when you need to find out without preparation. However if you are going to do that again, you can do like me: print the current time together with the shell prompt. That way, if you still have the terminal open, you can see the time when you started the program, and the time when the next prompt is printed. A little math will get you the execution time.

To do this in bash, put this in your .bashrc:

export PS1="A <a href="" class="__cf_email__" data-cfemail="aadfea">[email protected]</a>h W $ "

In zsh, put this in your .zshrc:
export PS1="%D{%H:%M} %<a href="" class="__cf_email__" data-cfemail="771937">[email protected]</a>%m %1~ %# "

The above will give your shell prompt a format of <time> <username>@<hostname> <current dir> <$ or % or #>. For different shells and crazy fancy prompts, read the man page of your shell.

Note: this probably won’t help if you need high precision, or if the program produces so much output that you can’t see the previous prompt.

Method 3

If the process is still running, you can use ps to get the time it started and how much CPU time it has used.

For example, for all processes:

ps -eo cputime,start,pid,command,args

For a particular pid (12345):
ps -p 12345 -o cputime,start,pid,command,args

Method 4

lastcomm (if BSD process accounting is enabled) will give you the execution duration (in some limited sense which may or may not be adequate).

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments