Print execution time of a shell command

Viewed 124136

Is is possible to print the execution time of a shell command with following combination?

root@hostname:~# "command to execute" && echo "execution time"
9 Answers

If you are using zshell, you can have zshell print the time @ the start and end of execution. You can accomplish this by adding the following in your ~/.zshrc:

# print time before & after every command
preexec() { eval THEDATE="`date +"[%D_%H:%M:%S] "`"; echo "<CMD STARTED> $THEDATE" } 
precmd()  { eval THEDATE="`date +"[%D_%H:%M:%S] "`"; echo "<CMD FINISHD> $THEDATE" }

and open a new terminal window to have the changes take effect in all future terminal sessions.

Just ps -o etime= -p "<your_process_pid>"

Related