如何知道Linux中的执行时间 [英] How to know the execution time in linux
本文介绍了如何知道Linux中的执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我要在名为launcher.sh的脚本中启动三个脚本(a.sh b.sh c.sh):
Suppose that I have three script(a.sh b.sh c.sh) to be launched in a script called launcher.sh:
first=`date +%s`
./a.sh &
./b.sh &
./c.sh &
final=`date +%s`
executionTime=`echo "scale=3;($final - $first)/3600" | bc`
echo $executionTime
我知道上面的脚本不起作用,因为launcher.sh将在启动a.sh,b.sh和c.sh之后立即完成.在没有&" 的情况下启动a.sh,b.sh和c.sh浪费了时间,因为它们不必彼此等待.
I know above script won't work because launcher.sh will finish immediately after launching a.sh, b.sh and c.sh. Launching a.sh, b.sh and c.sh without "&" is wasting time because they don't have to wait for each other.
假设它们的执行时间分别为5秒,10秒和7秒,那么我如何才能获得10秒的总执行时间呢?
Suppose that the execution time for them are 5 seconds, 10 seconds and 7 second respectively, how can I get 10 seconds as the overall execution time?
推荐答案
使用wait
:
first=`date +%s`
./a.sh &
./b.sh &
./c.sh &
wait
...
引用help wait
:
wait: wait [id]
Wait for job completion and return exit status.
Waits for the process identified by ID, which may be a process ID or a
job specification, and reports its termination status. If ID is not
given, waits for all currently active child processes, and the return
status is zero. If ID is a a job specification, waits for all processes
in the job's pipeline.
Exit Status:
Returns the status of ID; fails if ID is invalid or an invalid option is
given.
这篇关于如何知道Linux中的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文