一開始完全是為了貫徹"懶人的最高精神--懶"才會想要寫這個Shell Script的
可是當我寫完以後,卻發現...呃...沒有人想要用... (真的是太傷我的心了)
於是這又變成另一個完全就是自己寫得很開心但卻必須被打入冷宮的小程式 ... (真是搞不懂大家到底想要的是什麼...懶一點有什麼不好的)
不過,自己還是寫得很開心啦...(這種自我滿足的快樂,相信寫程式的你們都懂吧)
就當做是個練習 (呃...或是個紀念...v_v..)
搞不好以後還是會用得到,所以就放上來了....
#########程式由此開始#########
##############################
#!/bin/bash
# For testing - 2006.2.10 by Vicky
##########################################
# Auto-executed the following command
##########################################
#ProcName="httpd"
#ifMode="Auto"
##########################################
# Get the input program name from user
##########################################
echo -n "Please enter a program name:"
read ProcName
##########################################
#Get the result of query
##########################################
#RecData=`top -b -n 1 | grep $ProcName | awk '{echo -n system("date +%F-%T")};{print "PID= "$1 ", VM= "$5", CPU%= "$9", Mem%= "$10}'`
RecData=`top -b -n 1 | grep $ProcName `
if [ "$RecData" == "" ]; then
echo "Can not get the process!"
else
case $ifMode in
##########################################
#Auto-saved to a file.
##########################################
"Auto")
#Auto-saved the result to a file
# echo -n "[`date +%F-%T`] $RecData" >> \.\/$ProcName.monitor
top -b -n 1 | grep $ProcName | awk '{system("echo -n [`date +%F-%T`]");print " PID= "$1," VM= "$5,"CPU%= "$9,"Mem= "$10}' >> \.\/$ProcName.monitor
;;
##########################################
# User interactive interface
##########################################
*)
if [ "$RecData" == "" ]; then
echo "Can not get the record from the process!"
else
while [ "$yn" != "Y" ] && [ "$yn" != "y" ] && [ "$yn" != "N" ] && [ "$yn" != "n" ]
do
read -p "Output the result to screen? [Y/N]" yn
done
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
#echo "$RecData"
top -b -n 1 | grep $ProcName | awk '{system("echo -n [`date +%F-%T`]");print " PID= "$1,"VM= "$5,"CPU%= "$9,"Mem= "$10}'
exit 0
fi
if [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
#echo "$RecData" >> \.\/$ProcName.monitor
top -b -n 1 | grep $ProcName | awk '{system("echo -n [`date +%F-%T`]");print " PID= "$1,"VM= "$5,"CPU%= "$9,"Mem= "$10}' >> \.\/$ProcName.monitor
echo "Okay! Save the result to $ProcName.monitor file."
exit 0
fi
echo "Unexpected result! Please try again!!"
fi
;;
esac
fi
- Apr 17 Mon 2006 16:52
-
Get Process' information on Linux
請先 登入 以發表留言。