Linux – How to get the wall clock time of a running process

linuxprocesstime

I started a hash check of a large file and don't want to restart it using time. How can I get the wall clock time without using time at the beginning or using date right before I invoke a command?

Best Answer

For a running process you can do this:

PID=5462
command ps -p "$PID" -o etime
command ps -p "$PID" --no-headers -o etime

As a general feature you can modify your shell prompt. This is my bash prompt definition:

TERM_RED_START=$'\033[1m\033[31m'
TERM_RED_END=$'\033(B\033[m'
PS1='\nec:$(ec=$?; if [ 0 -eq $ec ];
then printf %-3d $ec;
else echo -n "$TERM_RED_START"; printf %-3d $ec; echo "$TERM_RED_END";
fi) \t  \u@\h:\w\nstart cmd:> '
PS2="cont. cmd:> "

The relevant part for you is the \t for the time.

This does not solve all problems, though. The new prompt will show when the process has ended but the former prompt may have been quite old when the command to be measured was started. So either you remember to renew the prompt before starting long running commands or you have to remember the current time when you want to know how long the current process will have taken.

For a complete solution you need an audit feature (which logs the start and end time of processes). But that may cause a huge amount of data if it cannot be restricted to the shell.

Related Question