我從這個命令嘗試
ps axr -o pid,time,command | grep batch_file.php > /tmp/view-process
回傳我:
21861 00:13:30 php -q batch_file.php
在檔案中/tmp/view-process
之后,我運行這 3 個命令:
PID=$(awk -F " " '{ print $1 }' /tmp/view-process)
TIME=$(awk -F " " '{ print $2 }' /tmp/view-process)
DIR=$(pwdx $PID > /tmp/view-process)
DIR=$(awk -F "/" '{ print $6 }' /tmp/view-process)
為了能夠僅檢索變數 $PID 中的 pid、$TIME 中的時間和 $DIR 中當前行程的路徑
我嘗試了幾種方法來執行 for 回圈或陣列,以便在一行上僅顯示 1 個 PID DIR TIME 并在第二行上顯示第二個 PID 等。
我只是設法做到了這一點:(21861 - proc_1 - 00:25:01對于 1 個 pid,它可以作業,但對于 2 個或更多,它不起作用。它的顯示如下:21861 23776 - proc_1 proc_2 - 00:12:02
因此,我找不到如何處理 for 回圈和我的檔案以獲得我的結果
你有什么想法 ?
uj5u.com熱心網友回復:
讓我們在沒有臨時檔案的情況下執行此操作。
我將使用 bash 關聯陣列和行程替換
declare -A times dirs
while read -r pid time cmd; do
times[$pid]=$time
done < <(
ps axr -o pid,time,command | grep batch_file.php
)
while IFS=': /' read -r pid _ _ _ _ _ dir _; do
dirs[$pid]=$dir
done < <(
pwdx "${!times[@]}"
)
for pid in "${!times[@]}"; do
echo "$pid - ${times[$pid]} - ${dirs[$pid]}"
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/478756.html
