Unix / Linux: Shell Stopwatch

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.

$ time read
$ time cat
$ 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.

$ 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.

$ dd

Whenever you need the elapsed time, send USR1 signal to the dd process using another terminal.

$ 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.

$ 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.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, perl, php, python, java, javascript, nodejs, angular, react); plays_on(xbox, ps4); linux_desktop_user(true); }