Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Answers to Frequently Asked Questions Tips and Tutorials My "Bread and Butter" Process Keep Alive Perl Script.... Post 302883288 by MadeInGermany on Monday 13th of January 2014 05:33:45 AM
Old 01-13-2014
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..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pid still alive after "control C"

hi , i'm running 2 similar script (ksh) under AIX , the first go normally out when i type "control c" : ps before : notes01 19882 38990 0 11:54:48 pts/0 0:00 recover -s cla0bkpe -d /base/base01 -t May 05 23:02:17 2005 -v -f -a /base/base01/mail/mail-01/xxxx.nsf notes01 30866 40880 0... (4 Replies)
Discussion started by: Nicol
4 Replies

2. Shell Programming and Scripting

How to include RETURN KEY with Background process "&" in Shell Script

Hello All, I am a newbie in Shell script programming, and maybe you can help me with my query. I need to write a shell script (mntServer.ksh) that will start a background process and also to be able to run another script. The mntServer.ksh script contains: #!/bin/ksh... (1 Reply)
Discussion started by: racbern
1 Replies

3. Shell Programming and Scripting

Startup Script "run process with Timer"

Hi I have a script that execute every X minute for checking new files in a folder and converting to pdf. Is there any way to start this script automatically on linux startup?. I use sleep function in script with infinite loop. while do killall -u `whoami` -q soffice soffice... (0 Replies)
Discussion started by: zawmn83
0 Replies

4. Shell Programming and Scripting

Shell script process remains after "exit 1"

I have a script that performs an oracle export: <snip> if then exp / full=y file=${exp_file} log=${exp_log} direct=y feedback=1000000 STATISTICS=NONE buffer=20000000 else exp / full=n owner=${schema_name} file=${exp_file} log=${exp_log} direct=y feedback=1000000... (4 Replies)
Discussion started by: Squeakygoose
4 Replies

5. Red Hat

"service" , "process" and " daemon" ?

Friends , Anybody plz tell me what is the basic difference between "service" , "process" and " daemon" ? Waiting for kind reply .. .. (1 Reply)
Discussion started by: shipon_97
1 Replies

6. Shell Programming and Scripting

Ksh script function, how to "EXIT 2" without killing the current process?

Hi, Using AIX 5.3 and Ksh. />ls -al /usr/bin/ksh -r-xr-xr-x 5 bin bin 237420 Apr 10 2007 /usr/bin/ksh /> I recently started working for a new employer. I have written UNIX K-Shell scripts for many years and have never had this particular issue before. Its perplexing me. I have... (2 Replies)
Discussion started by: troym72
2 Replies

7. Shell Programming and Scripting

Send "perl -e '...' " command through SSH, from a perl script

Hey guys I am trying to send a perl -e command to a number of systems using SSH. The command should retrieve some information for me. The problem is, the remote shell tries to interpolate my variables and doesn't get it should take the command literally and just execute it. Below the code.... (2 Replies)
Discussion started by: clrg
2 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies
IO::Async::PID(3pm)					User Contributed Perl Documentation				       IO::Async::PID(3pm)

NAME
"IO::Async::PID" - event callback on exit of a child process SYNOPSIS
use IO::Async::PID; use POSIX qw( WEXITSTATUS ); use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $kid = $loop->fork( code => sub { print "Child sleeping.. "; sleep 10; print "Child exiting "; return 20; }, ); print "Child process $kid started "; my $pid = IO::Async::PID->new( pid => $kid, on_exit => sub { my ( $self, $exitcode ) = @_; printf "Child process %d exited with status %d ", $self->pid, WEXITSTATUS($exitcode); }, ); $loop->add( $pid ); $loop->run; DESCRIPTION
This subclass of IO::Async::Notifier invokes its callback when a process exits. For most use cases, a IO::Async::Process object provides more control of setting up the process, connecting filehandles to it, sending data to and receiving data from it. EVENTS
The following events are invoked, either using subclass methods or CODE references in parameters: on_exit $exitcode Invoked when the watched process exits. PARAMETERS
The following named parameters may be passed to "new" or "configure": pid => INT The process ID to watch. Must be given before the object has been added to the containing "IO::Async::Loop" object. on_exit => CODE CODE reference for the "on_exit" event. Once the "on_exit" continuation has been invoked, the "IO::Async::PID" object is removed from the containing "IO::Async::Loop" object. METHODS
$process_id = $pid->pid Returns the underlying process ID $pid->kill( $signal ) Sends a signal to the process AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-10-24 IO::Async::PID(3pm)
All times are GMT -4. The time now is 09:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy