Watch a scripts CPU usage
The following script loops and creates a cpu load. The script itself is actually looking for itself in ps output and display it's own cpu usage every 1/2 second. If you make the sleep command lower, the script will use more cpu. :)
#!/bin/bash
stopme="stop_run";
stopmename="stop_runaway_processes.bash";
while [[ 0 -ne 1 ]]; do
wwwdatacpu=`ps -eo pcpu,pid,user,args | sort -k 1 -r | head -2 | tail -1 | grep $stopme | awk '{print $1}'`;
wwwdatapid=`ps -eo pcpu,pid,user,args | sort -k 1 -r | head -2 | tail -1 | grep $stopme | awk '{print $2}'`;
if [[ "$wwwdatacpu" == "" ]]; then
echo "there are no runaway ${stopmename} processes";
else
echo -n "${stopmename} is using ";
echo "${wwwdatacpu}% of the cpu on pid=${wwwdatapid}";
fi
touch file.txt;
echo "a bunch of stuff into the file" > file.txt;
grep "stuff into" file.txt >> /dev/null 2>&1;
#sleep .000001;
done
stopme="stop_run";
stopmename="stop_runaway_processes.bash";
while [[ 0 -ne 1 ]]; do
wwwdatacpu=`ps -eo pcpu,pid,user,args | sort -k 1 -r | head -2 | tail -1 | grep $stopme | awk '{print $1}'`;
wwwdatapid=`ps -eo pcpu,pid,user,args | sort -k 1 -r | head -2 | tail -1 | grep $stopme | awk '{print $2}'`;
if [[ "$wwwdatacpu" == "" ]]; then
echo "there are no runaway ${stopmename} processes";
else
echo -n "${stopmename} is using ";
echo "${wwwdatacpu}% of the cpu on pid=${wwwdatapid}";
fi
touch file.txt;
echo "a bunch of stuff into the file" > file.txt;
grep "stuff into" file.txt >> /dev/null 2>&1;
#sleep .000001;
done
wjs
[Click to add or edit comments])
Please prepend comments below including a date