April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

check process is running or not shell

!/bin/bash
CHECK=$0
SERVICE=$1
DATE=`date`
OUTPUT=$(ps aux | grep -v grep | grep -v $CHECK |grep $1)
echo $OUTPUT
if [ “${#OUTPUT}” -gt 0 ] ;
then echo “$DATE: $SERVICE service running, everything is fine”
else echo “$DATE: $SERVICE is not running”
fi

#!/bin/bash
ps axho comm| grep $1 > /dev/null
result=$?
echo “exit code: ${result}”
if [ “${result}” -eq “0” ] ; then
echo “`date`: $SERVICE service running, everything is fine”
else
echo “`date`: $SERVICE is not running”
/etc/init.d/$1 restart
fi

#!/bin/sh
SERVICE=$1
if ps ax | grep -v grep | grep -v $0 | grep $SERVICE > /dev/null
then
echo “$SERVICE service running, everything is fine”
else
echo “$SERVICE is not running”
fi

#!/bin/bash
ps_out=`ps -ef | grep $1 | grep -v ‘grep’ | grep -v $0`
result=$(echo $ps_out | grep “$1”)
if [[ “$result” != “” ]];then
echo “Running”
else
echo “Not Running”
fi

illall ${1} > /dev/null 2>&1 ;sleep 10

# — if we do not see the process, just end the function
pgrep ${1} > /dev/null 2>&1 || return

# — UGLY: Step trough every pid and use kill -9 on them individually
for PID in $(pidof ${1}) ;do

echo “Terminating Process: [${1}], PID [${PID}]”
kill -9 ${PID} ;sleep 10

# — NASTY: If kill -9 fails, try SIGTERM on PID
if ps -p ${PID} > /dev/null ;then
echo “${PID} is still running, forcefully terminating with SIGTERM”
kill -SIGTERM ${PID} ;sleep 10
fi

done

# — If after all that, we still see the process, report that to the screen.
pgrep ${1} > /dev/null 2>&1 && echo “Error, unable to terminate all or any of [${1}]” || echo “Terminate process [${1}] : SUCCESSFUL”

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>