-->
🏠 🔍
SHAREOLITE

Beginners Linux shell script to check whether a process is running or stopped

Below mentioned is a ready to use Linux script to check whether configured processes are running or stopped in the system, The script uses Linux ps command to capture the details . Copy the code below highlighted in blue , save it in a file and execute for output. 

There are two variables provided for customization. "Total_process" has the count of processes to be monitored. "processlist" has the list of processes to be monitored separated by comma.


#!/bin/sh
Total_process=2
processlist="process1,process2"
i=1
while [ $i -le $Total_process ]
do
p=`echo $processlist | cut -d ',' -f$i`
threads=`ps -ef | grep "$p" |grep -v grep |wc -l`
status=`ps -ef | grep "$p" |grep -v grep`
if [ $? -eq 1 ]
then
echo -e "\033[1;31mStopped $threads \033[0m\t - $p "
else
starttime=`ps -eo lstart,pid,cmd|grep "$p" | grep -v grep |awk '{print $3"-"$2"-"$5}'|head -1`
echo -e "\033[1;32mNormal $threads \033[0m\t - $starttime - $p"
fi
i=`echo $(($i+1))`
done

Hope it helps to some Linux beginners.
Comments

–>