How to log the time taken for a unix command?

2019-06-16 20:58发布

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 ?

4条回答
叛逆
2楼-- · 2019-06-16 21:39

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)"
查看更多
虎瘦雄心在
3楼-- · 2019-06-16 21:53

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
查看更多
叼着烟拽天下
4楼-- · 2019-06-16 21:55

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.

查看更多
成全新的幸福
5楼-- · 2019-06-16 21:59

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
查看更多
登录 后发表回答