Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question
    
I also always forgot this, and I've configured my prompt to display time. –  math Aug 9 '12 at 16:16
add comment

4 Answers 4

up vote 18 down vote accepted

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.

share|improve this answer
    
Perfect, thanks. –  Graham Borland Aug 3 '12 at 16:09
    
Awesome Mike, didn't even think this was possible. –  Tim Aug 3 '12 at 16:58
add comment

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.

share|improve this answer
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 '12 at 16:35
add comment

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
share|improve this answer
    
Thanks, but the process has completed already. I'll bear this in mind if I run it again. –  Graham Borland Aug 3 '12 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 '12 at 15:49
    
Also, keep in mind that cpu time is not "real time" –  Juan Aug 3 '12 at 23:25
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.