首頁>Linux>source

命令完成後,我希望得到如下報告:

Start Time: 02/01/21 01:27pm
End Time: 02/01/21 02:29pm
Total Duration: 01 Hour 02 Minutes

我發現可以使用 date +"%d/%m/%y %I:%M%P"获取開始時間和結束時間

例如,

start=$(date +"%d/%m/%y %I:%M%P")
start_duration=$(date +%s)
youtube-dl
end=$(date +"%d/%m/%y %I:%M%P")
end_duration=$(date +%s)
runtime=$((end_duration-start_duration))
echo "Start Time: ${start}"
echo "End Time: ${end}"

少數問题。

  1. 我不了解如何print Total Duration: 01 Hour 02 Minutes
  2. 如何使该指令碼通用,以便可以使用任何命令,而不仅仅是 youtube-dl 在此指令碼中。

我尝試過:

#! /bin/bash
Before=$(date +%s)
echo "$Before"
exec "[email protected]"
ret=$?
After=$(date +%s)
echo "$After"
let Duration=After-Before
echo "$Duration"
if [[ $ret -eq 0 ]]
then
    echo "Start Time: ${Before}"
    echo "End Time: ${After}"
    echo "Total Duration: ${Duration}"
    return $ret
else
    echo "Error: Command [ [email protected] ] returned $ret"
    echo "Start Time: ${Before}"
    echo "End Time: ${After}"
    echo "Total Duration: ${Duration}"
    return $ret
fi

但是如果我使用ctrl + c

則不起作用
$ ./reporttime tail -f ~/.xsession-errors
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
Cinnamon warning: Log level 128: posix_spawn avoided (fd close requested)
^C

實際上,在 exec "[email protected]"之後可能什麼也不執行

% ./reporttime cat reporttime
1609655953
#! /bin/bash
Before=$(date +%s)
echo "$Before"
exec "[email protected]"
ret=$?
After=$(date +%s)
echo "$After"
let Duration=After-Before
echo "$Duration"
if [[ $ret -eq 0 ]]
then
    echo "Start Time: ${Before}"
    echo "End Time: ${After}"
    echo "Total Duration: ${Duration}"
    return $ret
else
    echo "Error: Command [ [email protected] ] returned $ret"
    echo "Start Time: ${Before}"
    echo "End Time: ${After}"
    echo "Total Duration: ${Duration}"
    return $ret
fi%                                             
最新回復
  • 5月前
    1 #

    zsh

    zmodload zsh/datetime
    TIMEFMT='Total duration for %J: %*E'
    preexec() strftime 'Start Time: %d/%m/%y %I:%M%P'
    precmd() strftime 'End Time: %d/%m/%y %I:%M%P'
    REPORTTIME=0
    

    然後,您將获得輸入命令列的開始時間,顯示下一个提示之前的結束時間以及每个非內建命令的持續時間(更確切地說,對於等待 外壳):

    $ sleep 1; sleep 2
    Start Time: 02/01/21 08:31am
    Total duration for sleep 1: 1.002
    Total duration for sleep 2: 2.003
    End Time: 02/01/21 08:31am
    

    您可以更改 REPORTTIME=0REPORTTIME=10 例如,仅報告占用CPU時間超過10秒(而不是已花费時間)的命令的時間,或者為每个要計時的命令加 time前缀

    作為需要任意命令執行的指令碼:

    #! /bin/zsh -
    zmodload zsh/datetime
    TIMEFMT='Total duration: %*E'
    strftime -s start 'Start Time: %d/%m/%y %I:%M%P'
    {
      duration=$(
        exec 4>&2 2>&1 1>&3 3>&-
        time "[email protected]" 2>&4 4>&-
      )
    } 3>&1
    ret=$?
    strftime -s end 'End Time: %d/%m/%y %I:%M%P'
    print -rlu2 $start $end $duration
    exit $ret
    

    然後:

    $ ./reporttime sleep 65
    Start Time: 02/01/21 09:26am
    End Time: 02/01/21 09:26am
    Total duration: 1:05.00
    

    而不是依靠 time ,您也可以使用 $SECONDS$EPOCHSECONDS 特殊變數(或 $EPOCHREALTIME / $epochtime 如果您想要亚秒級的粒度)

    #! /bin/zsh -
    zmodload zsh/datetime
    start=($epochtime)
    "[email protected]"
    end=($epochtime) ret=$?
    strftime >&2 'Start Time: %FT%T.%3.%z' $start
    strftime >&2 'End Time: %FT%T.%3.%z' $end
    printf >&2 'Total duration: %.3f\n' $((
      end[1] - start[1] + (end[2] - start[2]) / 1e9
    ))
    exit $ret
    

    這次使用標準且明確的時間格式:

    $ ./reporttime2 sleep 1
    Start Time: 2021-01-02T09:50:49.261+0000
    End Time: 2021-01-02T09:50:50.264+0000
    Total duration: 1.003
    

    如果您仍然想報告按Ctrl+CCtrl+\時的計時 否則会杀死两个 並為命令計時,您可以添加:

    zsh
    
    在外壳程式的這些指令碼頂部的

    可以拦截這些訊號並且不会在那裏死掉.不要做 trap true INT QUIT 在這種情况下,這將匯致訊號被完全忽略並且定時命令不会被杀死。

    trap '' INT QUIT

  • ssh:在遠端服務器上執行命令時出現终端問题
  • arch linux:笔記本電脑關機的原因