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!

  • I also always forgot this, and I've configured my prompt to display time.
    – math
    Aug 9, 2012 at 16:16

4 Answers 4


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.

  • Awesome Mike, didn't even think this was possible.
    – Tim
    Aug 3, 2012 at 16:58

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 \u@\h \W \$ "

In zsh, put this in your .zshrc:

export PS1="%D{%H:%M} %n@%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.

  • 2
    I include a timestamp in my shell prompt for this reason, but beware that the time shown in the prompt is when the prompt was printed, which may be quite different from the time you actually finished typing and hit enter to start the command. In other words, remember to factor in "prompt idle time" yourself.
    – jw013
    Aug 3, 2012 at 16:35

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
  • Thanks, but the process has completed already. I'll bear this in mind if I run it again. Aug 3, 2012 at 15:49
  • I'm using GNU ps in linux, the ps in MacOSX might be BSD and I'm not sure of the arguments.
    – jsbillings
    Aug 3, 2012 at 15:49
  • Also, keep in mind that cpu time is not "real time"
    – Champo
    Aug 3, 2012 at 23:25

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .