I am looking for a command to track the time.
I imagine the following workflow:
- Run the command
- ...do something else...
- Return to the terminal window and stop the command
- As an output you receive the time elapsed between 3 and 1
I am looking for a command to track the time.
I imagine the following workflow:
This answer suggests to use
time cat
and use Ctrl-D to see the time.
You can also use
time read
and use Enter key
Also check this answer on the same question
cat
with ^D
instead of ^C
to exit with 0 instead of 130.
How about:
stopwatch() {
local start=$SECONDS
read -p "Hit Enter..."
echo $((SECONDS-start)) seconds elapsed
}
In action:
$ stopwatch
Hit Enter...
14 seconds elapsed
Simple way of doing it is to take start time, and once you stop the script - take stop time. Finally, print the time difference. This can easily be done with Python:
python -c $'import time;start=time.time();\ntry:\n\twhile True: time.sleep(0.25)\nexcept: print(time.time()-start)'
Or long version for readability:
import time
start=time.time();
try:
while True: time.sleep(0.25)
except:
print(time.time()-start)
Stopping is done with Ctrl+C
while True: pass
runs the CPU at 100%. I'd suggest while True: time.sleep(1e9)
instead (signal.pause()
works too but requires an additional import).
Nov 9, 2016 at 12:49
except KeyboardInterrupt
would be better
Nov 9, 2016 at 14:22