【问题标题】:How to get/check which parts/lines in bash script takes the most execution time?如何获取/检查 bash 脚本中的哪些部分/行花费最多的执行时间?
【发布时间】:2013-04-16 12:03:15
【问题描述】:

我有一个很长的 bash 脚本,它在后台执行一些操作(不产生任何输出)。

我想知道哪些行/部分占用了最多的执行时间。

我可以用某种工具/功能/linux内置实现吗?

【问题讨论】:

  • 不知何故它不会有帮助。在 redhat 6 上,我没有 /etc/bash.bashrc 只有 /etc/bashrc。即使我使用它而不是前者,我也没有得到跟踪。
  • 您必须将这些行放入您要分析的 bash 脚本中。

标签: bash shell execution-time


【解决方案1】:

我可以在这里想到两种方法。

1) 记录每个命令或函数的启动时间并输出到日志文件中

# echo "$(date) : Started function_x" >> $LOGFILE

2) 计算出每个函数/任务花费的秒数并记录下来。

startTime=$(date +%s)
longRunningFunction
endTime=$(date +%s)
echo "longRunningFunction took $(expr $endTime - $startTime) seconds" >> $LOGFILE 

【讨论】:

    【解决方案2】:

    当我过去不得不这样做时,我使用了这种格式:( time echo "hello" ) 2>>output.txt

    对脚本中的每一行执行此操作,以便为每一行计时。

    【讨论】:

      猜你喜欢
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 2012-10-30
      • 1970-01-01
      • 2018-01-10
      • 2017-08-04
      相关资源
      最近更新 更多