{"id":3335,"date":"2014-07-07T12:43:19","date_gmt":"2014-07-07T04:43:19","guid":{"rendered":"http:\/\/rmohan.com\/?p=3335"},"modified":"2014-07-07T14:44:31","modified_gmt":"2014-07-07T06:44:31","slug":"check-process-is-running-or-not-shell","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3335","title":{"rendered":"check  process is running or not shell"},"content":{"rendered":"<p>!\/bin\/bash<br \/>\nCHECK=$0<br \/>\nSERVICE=$1<br \/>\nDATE=`date`<br \/>\nOUTPUT=$(ps aux | grep -v grep | grep -v $CHECK |grep $1)<br \/>\necho $OUTPUT<br \/>\nif [ &#8220;${#OUTPUT}&#8221; -gt 0 ] ;<br \/>\nthen echo &#8220;$DATE: $SERVICE service running, everything is fine&#8221;<br \/>\nelse echo &#8220;$DATE: $SERVICE is not running&#8221;<br \/>\nfi<\/p>\n<p>#!\/bin\/bash<br \/>\nps axho comm| grep $1 > \/dev\/null<br \/>\nresult=$?<br \/>\necho &#8220;exit code: ${result}&#8221;<br \/>\nif [ &#8220;${result}&#8221; -eq &#8220;0&#8221; ] ; then<br \/>\necho &#8220;`date`: $SERVICE service running, everything is fine&#8221;<br \/>\nelse<br \/>\necho &#8220;`date`: $SERVICE is not running&#8221;<br \/>\n\/etc\/init.d\/$1 restart<br \/>\nfi<\/p>\n<p>#!\/bin\/sh<br \/>\nSERVICE=$1<br \/>\nif ps ax | grep -v grep | grep -v $0 | grep $SERVICE > \/dev\/null<br \/>\nthen<br \/>\n    echo &#8220;$SERVICE service running, everything is fine&#8221;<br \/>\nelse<br \/>\n    echo &#8220;$SERVICE is not running&#8221;<br \/>\nfi<\/p>\n<p>#!\/bin\/bash<br \/>\nps_out=`ps -ef | grep $1 | grep -v &#8216;grep&#8217; | grep -v $0`<br \/>\nresult=$(echo $ps_out | grep &#8220;$1&#8221;)<br \/>\nif [[ &#8220;$result&#8221; != &#8220;&#8221; ]];then<br \/>\n    echo &#8220;Running&#8221;<br \/>\nelse<br \/>\n    echo &#8220;Not Running&#8221;<br \/>\nfi<\/p>\n<p>illall ${1} > \/dev\/null 2>&#038;1 ;sleep 10<\/p>\n<p># &#8212; if we do not see the process, just end the function<br \/>\npgrep ${1} > \/dev\/null 2>&#038;1 || return<\/p>\n<p># &#8212; UGLY: Step trough every pid and use kill -9 on them individually<br \/>\nfor PID in $(pidof ${1}) ;do<\/p>\n<p>    echo &#8220;Terminating Process: [${1}], PID [${PID}]&#8221;<br \/>\n    kill -9 ${PID} ;sleep 10<\/p>\n<p>    # &#8212; NASTY: If kill -9 fails, try SIGTERM on PID<br \/>\n    if ps -p ${PID} > \/dev\/null ;then<br \/>\n        echo &#8220;${PID} is still running, forcefully terminating with SIGTERM&#8221;<br \/>\n        kill -SIGTERM ${PID}  ;sleep 10<br \/>\n    fi<\/p>\n<p>done<\/p>\n<p># &#8212; If after all that, we still see the process, report that to the screen.<br \/>\npgrep ${1} > \/dev\/null 2>&#038;1 &#038;&#038; echo &#8220;Error, unable to terminate all or any of [${1}]&#8221; || echo &#8220;Terminate process [${1}] : SUCCESSFUL&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>!\/bin\/bash CHECK=$0 SERVICE=$1 DATE=`date` OUTPUT=$(ps aux | grep -v grep | grep -v $CHECK |grep $1) echo $OUTPUT if [ &#8220;${#OUTPUT}&#8221; -gt 0 ] ; then echo &#8220;$DATE: $SERVICE service running, everything is fine&#8221; else echo &#8220;$DATE: $SERVICE is not running&#8221; fi<\/p>\n<p>#!\/bin\/bash ps axho comm| grep $1 > \/dev\/null result=$? echo &#8220;exit code: ${result}&#8221; [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3335"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3335"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3335\/revisions"}],"predecessor-version":[{"id":3336,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3335\/revisions\/3336"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}