Unix/Linux Go Back    


Tips and Tutorials Helpful articles from our Users.

My "Bread and Butter" Process Keep Alive Perl Script....

Tips and Tutorials


Tags
keep alive

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-08-2005   -   Original Discussion by Neo
Neo's Unix or Linux Image
Neo Neo is offline Forum Staff  
Administrator
 
Join Date: Sep 2000
Last Activity: 21 January 2018, 11:32 AM EST
Location: Asia pacific region
Posts: 14,293
Thanks: 960
Thanked 1,319 Times in 629 Posts
My "Bread and Butter" Process Keep Alive Perl Script....

For the newbies, I should have posted this years ago....

Here is the standard (tiny) "bread and butter" perl script (on Linux) I use in my crontab files to insure key processes are alive ( just in case ! ) like httpd, named, sshd, etc.

The example below if for named......



Code:
#!/usr/bin/perl

open(PS,"/bin/ps x|") || die "Can't Open PS";
while(<PS>) {
if (/usr\/sbin\/named/) { close PS; exit;}
}
close PS;
system("/usr/sbin/named");

One of my biggest "server fears" is that named will die for some strange reason, so I feel safe knowing that named is always running with the script above.

New Linux/UNIX users might find it useful.
Sponsored Links
    #2  
Old Unix and Linux 01-13-2014   -   Original Discussion by Neo
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 22 January 2018, 3:00 AM EST
Location: Simplicity
Posts: 3,947
Thanks: 335
Thanked 1,322 Times in 1,192 Posts
Now the haute cuisine:
a shell daemon that polls /proc every 30 seconds:


Code:
#!/bin/bash

# name of the daemon to watch
pname=named
pstart=/usr/sbin/$pname

# this script
me=${0##*/}
# e.g. me=named_watcher.sh

export PATH=/bin:/usr/bin:/usr/sbin:/sbin

# sh sends SIGHUP on exit, we need to continue
trap 'echo "SIGHUP captured"' HUP

# no interactions, log all output and errors
mkdir -p /var/log
exec </dev/null >/var/log/${me%.*}.log 2>&1

# Solaris needs -z option
zopt=`{ zonename; } 2>/dev/null`
zopt=${zopt:+"-z $zopt"}

echo "$me started with pid $$"
sleep 3
# initiate the pid variables
pid=`pgrep $zopt -x $pname`
[ -n "$pid" ] || exec echo "no $pname process - exiting"
echo "$me watching $pname[$pid]"

# endless loop
while :
do
 sleep 30
 if [ \! -d /proc/$pid ]; then
  echo "`date` $me $pid is gone!"
  sleep 1
  echo "starting $pname"
  date
  eval $pstart
  sleep 3
  # renew the pid variables
  pid=`pgrep $zopt -x $pname`
  [ -n "$pid" ] || exec echo "no $pname process - exiting"
  echo "$me watching $pname[$pid]"
 fi
done

The script should be background-started (with &) in the start section of the process (here: named) init script, *after* the process is started.
And killed in the stop section of the process start script, *before* the process is killed.

Last edited by MadeInGermany; 01-13-2014 at 03:46 PM..
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Ksh script function, how to "EXIT 2" without killing the current process? troym72 Shell Programming and Scripting 2 10-08-2009 05:32 PM
"service" , "process" and " daemon" ? shipon_97 Red Hat 1 05-24-2009 04:51 PM
Shell script process remains after "exit 1" Squeakygoose Shell Programming and Scripting 4 10-21-2008 12:17 PM
How to include RETURN KEY with Background process "&" in Shell Script racbern Shell Programming and Scripting 1 03-11-2008 08:30 AM
Pid still alive after "control C" Nicol Shell Programming and Scripting 4 05-11-2005 12:15 PM



All times are GMT -4. The time now is 04:35 AM.