【发布时间】:2015-08-04 06:55:46
【问题描述】:
我开发了一个脚本,通过给定的 PID(作为参数)查找正在运行的线程数。我们可以运行一次,一切都很完美。 但是,为了我们的需要,我必须让它以启动和停止选项运行。 意思是,直到我不让它停止脚本周记录线程号。 我试图在下面的代码中这样做,但似乎没有运气
PID=$1
Command=$2
scriptPID=`echo $$`
#going to check if arguments were supplied if not using the defaults. if only one argument supplied then aborting.
if [[ $PID -eq 0 ]]
then
echo -e "\n[ERROR]: No PID was provided. please provide PID as argument.\n"
exit 1
fi
initCommand(){
#local Command="$2";
case $Command in
start)
start "true"
;;
stop)
stop -f "true"
;;
esac
}
start(){
#going redirect stout & stderr to log file
echo "starting"
echo "script PID $scriptPID"
exec 1>>pidThreadNumber.log 2>&1
while (true); do
runningThreads=`ps huH p $PID | wc -l`
date;
echo "-------------------------------"
echo -e "Number of running threads: "$runningThreads"\n\n"
sleep 2
done
}
stop()
{
kill -9 $scriptPID
}
initCommand $Command
谢谢大家的帮助
【问题讨论】: