How to log the time taken for a unix command?

15,683

Solution 1

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

Solution 2

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.

Solution 3

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)"

Solution 4

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
Share:
15,683
alvas
Author by

alvas

食飽未?

Updated on July 28, 2022

Comments

  • alvas
    alvas almost 2 years

    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 ?