如何分析一个bash shell脚本? [英] How to profile a bash shell script?

查看:159
本文介绍了如何分析一个bash shell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的bash shell中最多需要3-4秒启动,而如果我用启动 - NORC 它会立即运行

My bash shell takes up to 3-4 seconds to start up, while if I start it with --norc it runs immediately.

我开始分析 /etc/bash.bashrc 〜/ .bashrc中通过手工插入收益语句和寻求速度的提高,但它不是一个定量的过程,它是没有效率。

I started "profiling" /etc/bash.bashrc and ~/.bashrc by manually inserting return statements and seeking for speed improvements, but it is not a quantitative process and it is not efficient.

如何分析我的bash脚本,看看哪些命令占据了大部分时间来启动?

How can I profile my bash scripts and see which commands take most time to start up?

推荐答案

如果您有GNU 日期(或者其他版本可以输出纳秒),这样做开头的 /etc/bash.bashrc (或任何你想要的任何Bash脚本,开始跟踪):

If you have GNU date (or another version that can output nanoseconds), do this at the beginning of /etc/bash.bashrc (or wherever you'd like to begin a trace in any Bash script):

PS4='+ $(date "+%s.%N")\011 '
exec 3>&2 2>/tmp/bashstart.$$.log
set -x

添加

set +x
exec 2>&3 3>&-

在年底〜/ .bashrc中(或任何Bash脚本的部分的结尾,你想跟踪停止)。

at the end of ~/.bashrc (or at the end of the section of any Bash script you'd like tracing to stop).

您应该得到一个跟踪日志 /tmp/bashstart.PID.log 显示已执行的每个命令的seconds.nanoseconds时间戳。从一个时间到下一个的差的时间,该中间步骤所花的量。

You should get a trace log in /tmp/bashstart.PID.log that shows the seconds.nanoseconds timestamp of each command that was executed. The difference from one time to the next is the amount of time that the intervening step took.

当你缩小范围,你可以移动设置-x 后和设置+ X 更早(或支架感兴趣的几个部分选择)。

As you narrow things down, you can move set -x later and set +x earlier (or bracket several sections of interest selectively).

这篇关于如何分析一个bash shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆