记录命令执行时间

发布于 2024-12-02 20:45:04 字数 391 浏览 2 评论 0原文

有一种可爱的方法可以在 Linux 控制台中记录所有命令的开始日期/时间。您应该将变量 HISTTIMEFORMAT 设置为“%F %T ”。然后通过运行“history”命令,您将看到类似以下内容:

  512  2011-09-02 22:57:41 export HISTTIMEFORMAT="%F %T "
  513  2011-09-02 22:57:42 ls
  514  2011-09-02 22:57:43 hist
  515  2011-09-02 22:57:45 history 

这非常酷且有用。但我的梦想是将命令执行时间也添加到此日志中。我知道我可以运行“time ./some_long_last_script”,但我不想每次都手动写入时间。也许有某种方法可以自动保存每个命令的执行时间?

There is a cute way of loggin the start date/time of all commands in linux console. You should set the variable HISTTIMEFORMAT to "%F %T ". Then by running 'history' command you will see something like:

  512  2011-09-02 22:57:41 export HISTTIMEFORMAT="%F %T "
  513  2011-09-02 22:57:42 ls
  514  2011-09-02 22:57:43 hist
  515  2011-09-02 22:57:45 history 

This is very cool and useful. But my dream is to add to this log also the command execution time. I know that I can run 'time ./some_long_lasting_script' but I don't want to write time every time manually. Maybe there is some way of auto saving every command execution time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2024-12-09 20:45:04

如果不太分散注意力,您可以更改提示以显示当前日期和时间。

如果您使用的是 bash,请考虑类似:

export PS1="[\D{%Y-%m-%d} \T][\u@\h \W]\$ "

看起来像:

[2011-09-03 03:39:21][james@fractal ~]$ echo $PS1
[\D{%Y-%m-%d} \T][\u@\h \W]$
[2011-09-03 03:39:30][james@fractal ~]$ vi
[2011-09-03 03:39:39][james@fractal ~]$ ping google.com
PING google.com (74.125.93.147) 56(84) bytes of data.
64 bytes from qw-in-f147.1e100.net (74.125.93.147): icmp_req=1 ttl=47 time=51.3 ms
^C
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 51.343/51.343/51.343/0.000 ms
[2011-09-03 03:39:44][james@fractal ~]$ 

当然,您可以根据您的个人喜好定制提示。

要永久更改提示,您可以将 export PS1=... 添加到 ~/.bashrc 文件中。或者,您可以编写别名来来回切换,例如,在您的 ~/.bashrc 文件中,

alias prompt_ts_on='export $PS1=...'
alias prompt_ts_off='export $PS1="[\u@\h \W]\$ "'

请注意 ... 应该是您选择的双引号附带的提示字符串。

关于 PS1 转义序列的一些参考:此处此处

If it wasn't too distracting, you could change your prompt to display the current date and time.

If you're using bash, consider something like:

export PS1="[\D{%Y-%m-%d} \T][\u@\h \W]\$ "

which would look like:

[2011-09-03 03:39:21][james@fractal ~]$ echo $PS1
[\D{%Y-%m-%d} \T][\u@\h \W]$
[2011-09-03 03:39:30][james@fractal ~]$ vi
[2011-09-03 03:39:39][james@fractal ~]$ ping google.com
PING google.com (74.125.93.147) 56(84) bytes of data.
64 bytes from qw-in-f147.1e100.net (74.125.93.147): icmp_req=1 ttl=47 time=51.3 ms
^C
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 51.343/51.343/51.343/0.000 ms
[2011-09-03 03:39:44][james@fractal ~]$ 

You could, of course, tailor your prompt to your personal preferences.

To permanently change your prompt you can add that export PS1=... to your ~/.bashrcfile. Alternatively, you could write aliases to toggle back and forth, for example, in your ~/.bashrc file,

alias prompt_ts_on='export $PS1=...'
alias prompt_ts_off='export $PS1="[\u@\h \W]\$ "'

Note that ... should be your choice of double-quote enclosed prompt string.

Some references on PS1 escape sequences: here and here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文