alvas
alvas

Reputation: 122052

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 ?

Upvotes: 10

Views: 17673

Answers (4)

Michael Kazarian
Michael Kazarian

Reputation: 4462

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.

Upvotes: 10

Jost
Jost

Reputation: 1569

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

Upvotes: 13

Mariappan Subramanian
Mariappan Subramanian

Reputation: 10063

At the beginning and ending your script you just need to have date commands which will log the information.

var1=`date`
echo "Starting of the script $var1" > timing_log.txt

<your code>

var2=`date`
echo "Ending of the script $var2"  >> timing_log.txt

Upvotes: 1

Hari Menon
Hari Menon

Reputation: 35405

I am not sure I get your question. time <command> will give the time taken by <command>. If you want the actual start and end times to be printed as well, you can do:

echo "start time = $(date)"
time <command>
echo "end time = $(date)"

Upvotes: 2

Related Questions