Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Tuesday, August 9, 2011

Make *nix automatic re-spawn a process/service if stopped


To make your *nix system automatic re-spawn a process/service if stopped ~

Write a shell script to check for presence of a service and start it if it is not running.
Then attach that shell-script to a cronjob running at frequent intervals.

Example Script to monitor Apache HTTPD web server and start it if is not started ~
#!/bin/sh
pids_=`ps ax | grep /usr/sbin/httpd | grep -v grep | wc -l `
if [ $pids_ -ne 0 ];
then
     echo "Apache is running"
else
     echo "Apache was stopped. Starting..."
     apache_start_=`/etc/init.d/httpd start`
     if [ $? -eq 0 ];
     then
         echo "Apache Started Successfully"
     fi
fi
#############################
 this simply checks for HTTPD process to be present and re-spawn the service in absence of even a single process thread.

There is a wonderful Ruby utility "bluepill" to achieve the same solution in a more advanced and comfortable manner. Cover it sooooon.....

Wednesday, August 11, 2010

[LiNUX] to kill multiple instances of a service; so AWKill (kill + awk) it

aw(Kill) all instances:

it has been happening in Linux where sometimes I need to start/restart/stop any Linux service but either it don't have a (init.d,rc.d,*)/Service Script or simply failed even if it had
then the usual thing I used to do is get Process ID of relevant App by using
`ps aux | grep $StuckApp` 
and then kill it by
`kill -9 $PID`

but few days back I ran a server (w/o any service script), generating 7-10 parallel instances of it
now to kill it look + type all PIDs... now 'm not lazy but that real wastage of time
so I automated it with help of AWK


so here is a command/script you can use to automate it in similar cases:

shell command:
ps aux | grep $StuckAppNameHere | awk '{print $2; system("kill -9 "$2"");}' 

to use it as a shell script save following two lines in a script:
echo "Killing all instances found matching for "$1
ps aux | grep $1 | awk '{print $2; system("kill -9 "$2"");}'


and pass the name of app to be matched and killed as parameter to it
say you saved script as awKillAll.sh, and you have to kill all processes matching with 'python'
then use
#awKillAll.sh python