【问题标题】:Track the time a command takes in UNIX/LINUX?跟踪命令在 UNIX/LINUX 中花费的时间?
【发布时间】:2012-08-19 06:48:54
【问题描述】:

在 UNIX/LINUX 中,是否有一种简单的方法来跟踪命令所用的时间?

【问题讨论】:

    标签: linux unix command-line terminal


    【解决方案1】:

    可以,使用time <command>,比如

    time ls
    

    更多选择请咨询man timeLink.

    【讨论】:

    • 而且,真实/用户/系统时间的含义很好地涵盖了here
    • 这个答案对于 linux 上的 bash 用户来说是不准确的。手册页记录了 Gnu time 命令,但 time 是 bash 中的内置命令,其中没有记录所有选项。
    【解决方案2】:

    使用

    /usr/bin/time 
    

    而不是 bash 中内置的时间:它是更可配置的 AFAIK。

    e.g. /usr/bin/time --format=' \n---- \nelapsed time is %e'ls
    

    【讨论】:

    • 据我所知,这是默认设置。我检查的 CentOS 6、CentOS 7 和 Debian 8 系统就是这种情况:user@host:~$ which time /usr/bin/time 看起来是 GNU 时间的 1.7 版。
    • @Toby:尽管“which”表示它是 /usr/bin/time,但在 bash 中,内置函数会覆盖它。如果我在 bash 中执行 time -f "\t%E real" ls,我会收到错误消息,但如果我执行 /usr/bin/time -f "\t%E real" ls,它会起作用。
    • 你是对的。这是非常有趣和启发性的。谢谢!
    • 不要使用which。使用type -a:$ which time /usr/bin/time $ type -a time time is a shell keyword time is /usr/bin/time
    • 请注意,使用 /usr/bin/time 会阻止您使用 bash 别名。为此需要 bash 内置 time,否则您将收到错误 cannot run my_alias: No such file or directory
    【解决方案3】:

    这是一秒钟的sleep 的样子,与time 计时:

    $ time sleep 1
    
    real    0m1.001s
    user    0m0.000s
    sys 0m0.000s
    

    【讨论】:

      猜你喜欢
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多