Posts

Showing posts with the label CPU load

Shell script - Kill a specific program if its CPU load exceeded a certain threshold

This shell script I wrote in order to kill the python process if its CPU usage percentage exceeded 96%: #! /bin/bash THRESHOLD=96 while [ 1 ]; do     MYPROGRAM_ID=`pidof python`     # if the result is not empty, or there is a python process     if [ ! -z "$ MYPROGRAM _ID" ] ; then         CPU_LOAD=`ps -o %C -p $ MYPROGRAM _ID | tail -n 1`         echo $CPU_LOAD         # bc will return 0 for false and 1 for true         if [ $(echo "$CPU_LOAD > $THRESHOLD" | bc) -ne 0 ] ; then             kill -9 $GATEKEEPER_ID         else             echo "Normal"         fi     fi done Useful!

Shell script - Get CPU usage percentage of a process

To get the CPU usage percentage of a specific process, run the following command: $ ps -o %C -p <PID> | tail -n 1 Example: to get CPU load of /usr/sbin/mysqld process which PID is 32241 : $ ps -o $C -p 32241 | tail -n 1 0.1