如何将最后一个命令的挂起时间放入 Bash 提示符中?
有没有办法将最后一个命令的运行时间嵌入到 Bash 提示符中?我希望看到这样的东西:
[last: 0s][/my/dir]$ sleep 10
[last: 10s][/my/dir]$
背景
我经常进行长时间的数据处理工作,知道它们花了多长时间很有用,这样我就可以估计未来需要多长时间工作机会。对于非常常规的任务,我会继续使用适当的日志记录技术严格记录这些信息。对于不太正式的任务,我只需在命令前面加上时间
。
如果能自动为每个交互式命令计时,并以几个字符而不是 3 行打印计时信息,那就太好了。
Is there a way to embed the last command's elapsed wall time in a Bash prompt? I'm hoping for something that would look like this:
[last: 0s][/my/dir]$ sleep 10
[last: 10s][/my/dir]$
Background
I often run long data-crunching jobs and it's useful to know how long they've taken so I can estimate how long it will take for future jobs. For very regular tasks, I go ahead and record this information rigorously using appropriate logging techniques. For less-formal tasks, I'll just prepend the command with time
.
It would be nice to automatically time
every single interactive command and have the timing information printed in a few characters rather than 3 lines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这是实现您想要的功能的最小独立代码:
This is minimal stand-alone code to achieve what you want:
根据您的回复和其他一些帖子,我写了这个提示,我想与您分享。我拍了一张屏幕截图,您可以看到:
这里是放入 ~/.bashrc 文件中的代码:
Using your replies and some other threads, I wrote this prompt which I want to share with you. I took a screenshot in wich you can see :
Here is the code to put in your ~/.bashrc file :
您可以将这个 zsh 借用的钩子用于 bash: http://www.twistedmatrix .com/users/glyph/preexec.bash.txt
使用此钩子完成的计时 (Mac OS X): 使用 Growl 监视长时间运行的 shell 命令
You could utilize this zsh-borrowed hook for bash: http://www.twistedmatrix.com/users/glyph/preexec.bash.txt
Timing done with this hook (Mac OS X): Use Growl to monitor long-running shell commands
另一个非常简单的方法是:
这显示自上一个简单命令启动以来的秒数。如果您只是按 Enter 键而不输入命令,则计数器不会重置 - 当您只想查看自上次在终端中执行任何操作以来终端已启动多长时间时,这会很方便。它在 Red Hat 和 Ubuntu 中对我来说工作得很好。它在 Cygwin 下对我不起作用,但我不确定这是一个错误还是只是尝试在 Windows 下运行 Bash 的限制。
这种方法的一个可能的缺点是您不断重置 SECONDS,但如果您确实需要将 SECONDS 保留为自初始 shell 调用以来的秒数,您可以为 PS1 计数器创建自己的变量,而不是直接使用 SECONDS。另一个可能的缺点是,诸如“999999”之类的大秒值可能会更好地显示为天+小时+分钟+秒,但很容易添加一个简单的过滤器,例如:
这会将“999999”转换为“11天,13: 46:39”。最后的 sed 将“1days”更改为“1day”,并修剪掉空的前导值,例如“0days,00:”。调整口味。
Another very minimal approach is:
This shows the number of seconds since the last simple command was started. The counter is not reset if you simply hit Enter without entering a command -- which can be handy when you just want to see how long the terminal has been up since you last did anything in it. It works fine for me in Red Hat and Ubuntu. It did NOT work for me under Cygwin, but I'm not sure if that's a bug or just a limitation of trying to run Bash under Windows.
One possible drawback to this approach is that you keep resetting SECONDS, but if you truly need to preserve SECONDS as the number of seconds since initial shell invocation, you can create your own variable for the PS1 counter instead of using SECONDS directly. Another possible drawback is that a large seconds value such as "999999" might be be better displayed as days+hours+minutes+seconds, but it's easy to add a simple filter such as:
This translates "999999" into "11days,13:46:39". The sed at the end changes "1days" to "1day", and trims off empty leading values such as "0days,00:". Adjust to taste.
如果您在开始长时间运行的工作之前没有设置任何其他答案,并且您只想知道该工作花了多长时间,您可以做简单的事情
,它会回复类似的内容
,然后您可以直观地减去两个时间戳(有人知道如何捕获 shell 内置历史命令的输出吗?)
If you hadn't set up any of the other answers before you kicked off your long-running job and you just want to know how long the job took, you can do the simple
and it will reply with something like
and you can then just visually subtract the two timestamps (anybody know how to capture the output of the shell builtin history command?)
我从 Ville Laurikari 那里得到了答案,并使用
time
命令改进了它,以显示亚秒级精度:当然,这需要启动一个进程,因此效率较低,但仍然足够快,您不会没注意到。
I took the answer from Ville Laurikari and improved it using the
time
command to show sub-second accuracy:Of course this requires a process to be started, so it's less efficient, but still fast enough that you wouldn't notice.
这是我对 Thomas
使用
date +%s%3N
获取毫秒作为基本单位的看法,简化以下代码(减少零)
然后将
$t_show
添加到您的 PS1Here's my take on Thomas'
uses
date +%s%3N
to get milliseconds as base unit,simplified following code (less zeros)
Then add
$t_show
to your PS1我发现每次调用
$PROMPT_COMMAND
时都会运行trap ... DEBUG
,重置计时器,因此总是返回 0。但是,我发现
history
记录时间,我利用这些来得到答案:但这并不完美:
history
没有注册命令(例如重复或忽略的命令),则开始时间将为错误的。历史记录
中正确提取日期。I found that
trap ... DEBUG
was running every time$PROMPT_COMMAND
was called, resetting the timer, and therefore always returning 0.However, I found that
history
records times, and I tapped into these to get my answer:It's not perfect though:
history
doesn't register the command (e.g. repeated or ignored commands), the start time will be wrong.history
.zsh
的翻译版本。附加到您的
~/.zshrc
文件Translated version for
zsh
.Append to your
~/.zshrc
filebash 4.x 及更高版本的另一种方法是将
coproc
与PS0
和PS1
一起使用,如下所示:这是一个
.bashrc
准备好的片段。它对于使用 undistract-me 的每个人来说特别有用,它会覆盖
trap DEBUG 出于其自身目的。
Another approach for bash 4.x and above would be to use
coproc
withPS0
andPS1
like below:This is a
.bashrc
ready snippet.It is especially useful for everyone that uses undistract-me which overwrites
trap DEBUG
for its own purposes.如果有人只是想看看执行时间
将此行添加到 bash_profile
If somone just wants to see the time of execution,
add this line to bash_profile
受 zsh spaceship 提示符启发的分时、分、秒版本,基于 Ville的回答和这次转换函数< /a> 佩雷亚尔。
我还添加了一个阈值变量,以便计时器仅显示长时间运行的命令。
对于我的屏幕截图中的彩色输出:
A version with split hours, minutes and seconds inspired by the zsh spaceship prompt, based on Ville's answer and this time conversion function by perreal.
I also added a threshold variable so that the timer only displays for long running commands.
For the coloured output in my screenshot:
在 PS1 中添加 \t 对你有用吗?
它不会给出经过的时间,但在必要时应该很容易减去时间。
根据OP的评论,他已经在使用\t。
如果您可以使用 tcsh 而不是 bash,则可以设置时间变量。
您可以将打印格式更改为不那么难看(参见 tcsh 手册页)。
我不知道 bash 中有类似的功能
Will putting a \t in PS1 work for you?
It does not give the elapsed time but it should be easy enough to subtract the times when necessary.
Following the OP's comment that he is already using \t.
If you can use tcsh instead of bash, you can set the time variable.
You can change the format of the printing to be less ugly (se the tcsh man page).
I do not know of a similar facility in bash
这是我的版本
示例输出:
this is my version
sample output: