1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > linux计算脚本执行时间 linux – 计算shell脚本每一步的时间并显示总执行时间

linux计算脚本执行时间 linux – 计算shell脚本每一步的时间并显示总执行时间

时间:2018-11-22 21:18:24

相关推荐

linux计算脚本执行时间 linux  – 计算shell脚本每一步的时间并显示总执行时间

我有下面的脚本,并且要求我必须为每个脚本放置一些函数来获取每个脚本的时间信息,并最后显示总时间.

我的主要脚本如下所示:

/u01/scripts/stop.sh ${1} | tee ${stop_log}

/u01/scripts/kill_proc.sh ${1} | tee ${kill_log}

/u01/scripts/detach.sh ${1}| tee ${detach_log}

/u01/scripts/copy.sh ${1} | tee ${copy_log}

我想使用类似下面的函数来获取每个脚本执行时间,最后使用全局变量我可以显示所有脚本花费的总时间.

我在下面创建但不幸的是我无法正常使用,如果你在这里有一些善意的帮助.

time_check()

{

export time_log=${log}/time_log_${dts}.log

echo 'StartingTime:'date +%s > ${time_log}

echo 'EndingTime:'date +%s >> ${time_log}

}

我想使用类似于上面的函数来获取每个脚本执行时间,最后使用全局变量我可以显示所有脚本所花费的总时间.任何人都可以指导如何获得所需的结果.

解决方法:

如果您对时间粒度为秒的确定,则可以执行以下操作:

start=$SECONDS

/u01/scripts/stop.sh ${1} | tee ${stop_log}

stop=$SECONDS

/u01/scripts/kill_proc.sh ${1} | tee ${kill_log}

kill_proc=$SECONDS

/u01/scripts/detach.sh ${1}| tee ${detach_log}

detach=$SECONDS

/u01/scripts/copy.sh ${1} | tee ${copy_log}

end=$SECONDS

printf "%s\n" "stop=$((stop-start)), kill_proc=$((kill_proc-stop)), detach=$((detach-kill_proc)), copy=$((end-detach)), total=$((end-start))"

您也可以编写一个函数来执行此操作:

time_it() {

local start=$SECONDS rc

echo "$(date): Starting $*"

"$@"; rc=$?

echo "$(date): Finished $*; elapsed = $((SECONDS-start)) seconds"

return $rc

}

使用Bash版本> = 4.2,您可以使用printf打印日期而不是调用外部命令:

time_it() {

local start=$SECONDS ts rc

printf -v ts '%(%Y-%m-%d_%H:%M:%S)T' -1

printf '%s\n' "$ts Starting $*"

"$@"; rc=$?

printf -v ts '%(%Y-%m-%d_%H:%M:%S)T' -1

printf '%s\n' "$ts Finished $*; elapsed = $((SECONDS-start)) seconds"

return $rc

}

并将其调用为:

start=$SECONDS

time_it /u01/scripts/stop.sh ${1} | tee ${stop_log}

time_it /u01/scripts/kill_proc.sh ${1} | tee ${kill_log}

time_it /u01/scripts/detach.sh ${1}| tee ${detach_log}

time_it /u01/scripts/copy.sh ${1} | tee ${copy_log}

echo "Total time = $((SECONDS-start)) seconds"

有关:

标签:bash,shell,unix,linux,scripting

来源: https://codeday.me/bug/0701/1346558.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。