Objective: Time an event or something using the shell on Unix or Linux.
The time
command can be used to time any events. Use one of the following commands to start the timer.
1 |
$ time read |
1 |
$ time cat |
1 |
$ time dd |
The time command will keep the time and will print the elapsed time when the program (we are using the commands read
, cat
and dd
) passed to it exits. Since the commands are waiting for a user input, we have the flexibility to determine when to stop the timer – by giving the required user input to end the commands.
To stop the timer and get the elapsed time, press the enter key for the first command syntax (using time read
). For the remaining commands, press Ctrl-C
and you will be given the elapsed time. An example of the output is shown below.
1 2 3 4 5 |
$ time read real 0m3.584s user 0m0.000s sys 0m0.000s |
If you need to keep track of time at specific intervals, you can use the dd
command. Start the timer by running dd
.
1 |
$ dd |
Whenever you need the elapsed time, send USR1
signal to the dd
process using another terminal.
1 |
$ pkill -USR1 dd |
This will inform the dd
process to print out the statistics, one of them being the time in seconds. The output will be similar to the one found below. The USR1
signal was sent at around 2.75 and 10.19 seconds.
1 2 3 4 5 6 7 |
$ dd 0+0 records in 0+0 records out 0 bytes (0 B) copied, 2.75606 s, 0.0 kB/s 0+0 records in 0+0 records out 0 bytes (0 B) copied, 10.1951 s, 0.0 kB/s |
To stop the timer and end the dd
process, press Ctrl-C
in the terminal running the dd process.