我對shell腳本非常陌生,我試圖獲取進程的內存和CPU利用率。將來我會從java程序運行這個腳本,以便我可以使用該java程序的所有子進程。我的劇本目前看起來像作爲無法使用shell腳本獲取進程信息
#!/bin/bash
# get the process Id of the program executing this shell script
ID=$PPID
echo the process id of the parent of this script is $ID
#fetch the parent of the program which executed this shell
PID=`ps -o ppid=$ID`
echo the grand parent id of the script is $PID
# traverse through all children and collect there memory/cpu utiliation information
for p in `pgrep -P $PID`; do
top -c -n 1 -p $p > /tmp/stat.log
done
現在,當我運行這個程序我得到以下輸出
the process id of the parent of this script is 5331
the grand parent id of the script is 5331 3173 5331 6174
top: bad pid 'Usage:'
top: bad pid 'pgrep'
top: bad pid '[-flvx]'
top: bad pid '[-d'
top: bad pid 'DELIM]'
top: bad pid '[-n|-o]'
top: bad pid '[-P'
top: bad pid 'PPIDLIST]'
top: bad pid '[-g'
top: bad pid 'PGRPLIST]'
top: bad pid '[-s'
top: bad pid 'SIDLIST]'
top: bad pid '[-u'
top: bad pid 'EUIDLIST]'
top: bad pid '[-U'
top: bad pid 'UIDLIST]'
top: bad pid '[-G'
top: bad pid 'GIDLIST]'
top: bad pid '[-t'
top: bad pid 'TERMLIST]'
top: bad pid '[PATTERN]'
可能有人請幫助我。
我很抱歉,但就像我說的,你必須給'PPID ='和'「$ ID」'之間的空間: 'PID = $(ps -o ppid =「$ ID」| egrep'\ S +')'。哦,是的,我忘了你必須添加'-o':'PID = $(ps -o ppid =「$ ID」| egrep -o'\ S +')' – konsolebox
另外,如果你真的使用bash, <(exec ps -o ppid =「$ ID」)'可能會更好。 – konsolebox