How to log the time taken for a unix command?

て烟熏妆下的殇ゞ 提交于 2019-12-12 10:36:40

问题


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 ?


回答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



回答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.




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



回答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


来源:https://stackoverflow.com/questions/18781264/how-to-log-the-time-taken-for-a-unix-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!