Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log the time taken for a unix command?

I know my script is going to take more than 10 hours to run. Is there a way to log the time it starts and the time it ends ?

Does the time command just time the process or do I get the output of the process that I'm timing ?

like image 946
alvas Avatar asked Sep 13 '13 08:09

alvas


People also ask

How do you find the time taken to execute a command in Linux?

command time works in most shells. /usr/bin/time should work in all shells. you can change the output of the system time . use -p to get output similar to the shell builtin time . use -f to write your own format.

How does Unix measure time?

Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC.

How can you know the execution time of a command?

On Unix-like systems, there is a utility named 'GNU time' that is specifically designed for this purpose. Using Time utility, we can easily measure the total execution time of a command or program in Linux operating systems.

How is script execution time calculated in Unix?

You can use ' time ' command to get the execution time of the script. This command will call the Tcl interpreter count times to evaluate script (or once if count is not specified).


2 Answers

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
like image 53
Jost Avatar answered Oct 02 '22 22:10

Jost


The time command shows how long your process runs:

$ time sleep 2

real    0m2.002s
user    0m0.000s
sys 0m0.000s
$  

sleep 2 is just a simple process that takes 2 seconds.

To log the current time, use the date command.

like image 22
Michael Kazarian Avatar answered Oct 02 '22 21:10

Michael Kazarian