我有一個在背景執行的腳本,並使用以下命令捕獲所有輸出:
nohup bash -x test.sh < /dev/null > log.txt 2>&1 &
我怎樣才能知道完成需要多長時間?如果我在收到錯誤time
後嘗試使用:bash -x
/usr/bin/time: /usr/bin/time: cannot execute binary file
如果我嘗試使用acct
它似乎不會記錄該過程或我找不到它。
答案1
[這假設您希望 to 的輸出time
也轉到log.txt
,這從您的問題中不清楚]
只是不要使用nohup
。
nohup
沒有做任何特別的事情,只是忽略SIGHUP
信號,將 stdout 和 stderr 重定向到文件,並且 - 僅在某些變體中,例如nohup
來自 GNU coreutils - 將標準輸入從/dev/null
打開的位置重定向只寫模式。
您可以輕鬆地從 shell 完成所有這些操作:
{ trap '' HUP; time bash -x your_script args ... ; } > log.txt 2>&1 0>/dev/null &
或者更安全的是,如果從腳本啟動或您不打算使用以下命令將命令返回到前台fg
:
(trap '' HUP; time bash -x your_script args ... 0>/dev/null &) > log.txt 2>&1
0>/dev/null
(如果您不想模擬 GNU nohup 的該功能,則可以省略)。
當然,如果出於某種難以理解的原因,您將神奇的屬性賦予了 nohup,您也可以將第一個範例中的語法技巧與 nohup 一起使用:
{ time nohup bash -x your_script args ... ; } > log.txt 2>&1 &
答案2
只是time
過程nohup
:
$ time nohup bash -x ~/scripts/foo.sh
nohup: ignoring input and appending output to 'nohup.out'
real 0m10.011s
user 0m0.005s
sys 0m0.006s
foo.sh
只是舉sleep 10
個例子。
答案3
你可以只使用:
time yourscript.sh
但如果這對您不起作用,您可以將腳本內容包裝如下:
STARTTIME=$(date +%s)
#command block that takes time to complete...
ENDTIME=$(date +%s)
echo "It takes $($ENDTIME - $STARTTIME) seconds to complete this task..."