Creating a daemon to run in background

I am trying to create a service to always run and monitor a script that has a tendency to hang, we could not find what is causing it to hang so are in the process of completely reprogramming just about everything, however, that will take upto 6 months.

So I need to create this to monitor the script to make sure it does not run wild which it does once in a while, sometimes only once per month, once per week or sometimes once per day or hour.

So here is what I am trying to do:

Get this code:

ps auxww | egrep [m]ember.cgi | awk '{print $2}' | while read PID; do kill -9 $PID; done;

to always be running in the background. I need the script to be allowed to run for at least 2 seconds before it would kill it, but i have no idea how to do that... Here is what I have so far and it does not work at all...

#!/bin/sh
#Kill_daemon.sh

i=0

while [$i -le 12]
do
ps auxww | egrep [m]ember.cgi | awk '{print $2}' | while read PID; do kill -9 $PID; done;
i++
sleep 5

done

however, when I try to execute it, I get this error:
: command not foundh: line 3:
: command not foundh: line 5:
Kill_daemon.sh: line 15: syntax error: unexpected end of file

I would appreciate any assistance you can provide, even if you can just point me in the correct direction. I am a novice in shell programming, I do not know much at all about it. I am a perl programmer and wrote a perl script to do it, however, when I keep it running, it holds the memory and cpu usage while sleeping, so the server load does not go down below .40 which if I don't have any of the scripts running wild and no perl script running to monitor it the server load gets down to like .10

Thank you much,
Richard

Could this help you ?
Put space after [ and before ] in while ,

while [ $i -le 12 ]

and change i++ to

i=`expr $i + 1`
while true
do
 ps auxww | egrep [m]ember.cgi | awk '{print $2}' | 
 while read PID; 
 do 
    ( sleep 2 && kill -0 $PID && kill -9 $PID )  &  # wait 2 secs, test for pid again, kill it
 done;
 wait               # wait for all of the  ( sleep 2 .... ) & above child processes to complete
 sleep 1           # give the cpu a break
done

This whole approach is not a great idea. Shell daemons have security issues, for one thing. I added a sleep 1 so this thing is not gobbling up cpu all day. The wait statement makes the process wait until all of the pids have been processed.

The while true loop is an infinite loop. Instead of that loop consider using cron, maybe an entry like this:

* * * * * /path/to/Kill_Daemon.sh 

This runs the code every minute. You can lose the while true loop and the sleep 1 if you do this.

It may be advisible to run the cron say every 10 minutes to allow the fault time to develop and to avoid overlapping kill scripts.

2,12,22,32,42,52 * * * * /path/to/Kill_Daemon.sh

@ukndoit
Normally I would have a rant about using "kill -9" (instead of a tidier kill command) but I recognise that orphan and looping ".cgi" processes are common and somewhat difficult to kill.

Thanks guys, I appreciate it. I was given this code to just put inside those cgi scripts since they are simple redirect scripts for the replication that verifies a members username and then redirects them to the appropriate page, it should be done instantly, so for sure within 2 seconds, so what we did was add a simple alarm to the top of the script to tell it after 3 seconds process the alarm, which processes a subroutine to kill the current process... I added that to the scripts and it worked. So I do not need to create this now.

I really appreciate all your help.
Thank you for your time. I did learn something about shell too. thanks for that.

Richard

Kill_Daemon.sh might be simplified that way:

for pid in $(pgrep -f [m]ember.cgi;sleep 2)
do
    kill -9 $pid 2>/dev/null
done