Track the Time a Command Takes in Unix/Linux

Track the time a command takes in UNIX/LINUX?

Yes, use time <command>, such as

time ls

Consult man time for more options. Link.

How to log the time taken for a unix command?

Use the time command (details):

time your_prog

If time does not fit for you, I would try to log the output of date (details) before and after the execution of your program, e.g.

date > log.txt; your_prog; date >> log.txt

Finally, you can also add some formatting (NOTE: inspired by Raze2dust's answer):

echo "started at: $(date)" > log.txt; your_prog; echo "ended at: $(date)" >> log.txt

Date and time calculations to track time using command line in Unix/Linux

wilhelmtell has a great script, if you need to use a slightly modified version so that you can continue to use $(date) instead of $(date +%s) this will work in an identical fashion, just with an added line to reformat the times using +%s

#!/bin/bash
total=0
while read LINE; do
d1=$(echo $LINE |cut -c1-28);
t1=$(date -d "$d1" +%s);
read LINE
d2=$(echo $LINE |cut -c1-28);
t2=$(date -d "$d2" +%s);
total=$(($total+$t2-$t1));
done
echo $(($total/(60*60))) hours

Get program execution time in the shell

Use the built-in time keyword:


$ help time

time: time [-p] PIPELINE
Execute PIPELINE and print a summary of the real time, user CPU time,
and system CPU time spent executing PIPELINE when it terminates.
The return status is the return status of PIPELINE. The `-p' option
prints the timing summary in a slightly different format. This uses
the value of the TIMEFORMAT variable as the output format.

Example:

$ time sleep 2

real 0m2.009s
user 0m0.000s
sys 0m0.004s

Is there a way to find the running time of the last executed command in the shell?

I do not know, how it is in bash, but in zsh you can define preexec and precmd functions so that they save the current time to variables $STARTTIME (preexec) and $ENDTIME (precmd) so you will be able to find the approximate running time. Or you can define an accept-line function so that it will prepend time before each command.

UPDATE:
This is the code, which will store elapsed times in the $_elapsed array:

preexec () {
(( $#_elapsed > 1000 )) && set -A _elapsed $_elapsed[-1000,-1]
typeset -ig _start=SECONDS
}
precmd() { set -A _elapsed $_elapsed $(( SECONDS-_start )) }

Then if you run sleep 10s:

% set -A _elapsed # Clear the times
% sleep 10s
% sleep 2s ; echo $_elapsed[-1]
10
% echo $_elapsed
0 10 0

No need in four variables. No problems with names or additional delays. Just note that $_elapsed array may grow very big, so you need to delete the first items (this is done with the following piece of code: (( $#_elapsed > 1000 )) && set -A _elapsed $_elapsed[-1000,-1]).

UPDATE2:
Found the script to support zsh-style precmd and preexec in bash. Maybe you will need to remove typeset -ig (I used just to force $_start to be integer) and replace set -A var ... with var=( ... ) in order to get this working. And I do not know how to slice arrays and get their length in bash.

Script: http://www.twistedmatrix.com/users/glyph/preexec.bash.txt (web.archive)

UPDATE3:
Found one problem: if you hit return with an empty line preexec does not run, while precmd does, so you will get meaningless values in $_elapsed array. In order to fix this replace the precmd function with the following code:

precmd () {
(( _start >= 0 )) && set -A _elapsed $_elapsed $(( SECONDS-_start ))
_start=-1
}

How to redirect the output of the time command to a file in Linux?

Try

{ time sleep 1 ; } 2> time.txt

which combines the STDERR of "time" and your command into time.txt

Or use

{ time sleep 1 2> sleep.stderr ; } 2> time.txt

which puts STDERR from "sleep" into the file "sleep.stderr" and only STDERR from "time" goes into "time.txt"

Print execution time of a shell command

Don't forget that there is a difference between bash's builtin time (which should be called by default when you do time command) and /usr/bin/time (which should require you to call it by its full path).

The builtin time always prints to stderr, but /usr/bin/time will allow you to send time's output to a specific file, so you do not interfere with the executed command's stderr stream. Also, /usr/bin/time's format is configurable on the command line or by the environment variable TIME, whereas bash's builtin time format is only configured by the TIMEFORMAT environment variable.

$ time factor 1234567889234567891 # builtin
1234567889234567891: 142662263 8653780357

real 0m3.194s
user 0m1.596s
sys 0m0.004s
$ /usr/bin/time factor 1234567889234567891
1234567889234567891: 142662263 8653780357
1.54user 0.00system 0:02.69elapsed 57%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+215minor)pagefaults 0swaps
$ /usr/bin/time -o timed factor 1234567889234567891 # log to file `timed`
1234567889234567891: 142662263 8653780357
$ cat timed
1.56user 0.02system 0:02.49elapsed 63%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+217minor)pagefaults 0swaps

Overhead of time command in unix

The overhead is fixed and, based on the source code, is only due to the fact that an extra process is being started (the time process itself), introducing a small amount of extra processing (a). Normally, the shell would start your program but, in this case, the shell starts time and time starts your process (with a fork).

This extra processing involves:

  • argument processing.
  • the time taken to fork and exec the child.

While the process being measured is running, time itself is simply waiting for it to exit (with a wait call) so has no impact on the process.

So, while the start-up time for the time process is actually included in the measurements, these will only be significant for very short processes. If your process runs for an appreciable amount of time, the overhead of time is irrelevant.

As to what I mean by appreciable, you can see the effect time has by running it with a very fast executable, and also see if it has any appreciable increase in overhead for longer-running processes:

pax> time sleep 0
real 0m0.001s
user 0m0.000s
sys 0m0.000s

pax> time sleep 1
real 0m1.001s
user 0m0.000s
sys 0m0.000s

pax> time sleep 10
real 0m10.001s
user 0m0.000s
sys 0m0.004s

pax> time sleep 100
real 1m40.001s
user 0m0.000s
sys 0m0.000s

In other words, hardly any effect at all.

Now, since you're only likely to be timing processes if they're long-running (it's hard to care whether a single process takes one or two milliseconds unless you're running it many times in succession, in which case there are better ways to increase performance), the fixed overhead of time gets less and less important.


(a): And, if you're using a shell with time built in (such as bash with its time reserved word), even that small overhead disappears.

How can I get the current date and time in the terminal and set a custom command in the terminal for it?

The command is date

To customise the output there are a myriad of options available, see date --help for a list.

For example, date '+%A %W %Y %X' gives Tuesday 34 2013 08:04:22 which is the name of the day of the week, the week number, the year and the time.

Linux CMD - How to measure the time for two commands after each other?

Just use parantheses:

# time sleep 1; sleep 1     # <-- this does not work

real 0m1.003s
user 0m0.001s
sys 0m0.001s
# time (sleep 1; sleep 1) # <-- this one works

real 0m2.004s
user 0m0.001s
sys 0m0.002s

I would suggest though to use && instead of ;, like

# time (sleep 1 && sleep 1)

This way the second command is only executed when the first one was successful. With ; both commands are executed no matter what. Just my preference.



Related Topics



Leave a reply



Submit