LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > *BSD
User Name
Password
*BSD This forum is for the discussion of all BSD variants.
FreeBSD, OpenBSD, NetBSD, etc.

Notices


Reply
  Search this Thread
Old 03-25-2004, 12:02 PM   #1
rspurlock
LQ Newbie
 
Registered: Jul 2003
Location: Knoxville, TN
Posts: 13

Rep: Reputation: 0
script to check if process is dead or running


I'm trying to write either a perl script or shell script to restart natd after an html script adds to the natd.conf file. The problem I've run into is after the script kills the natd process, it has to quess how long to wait before it restarts it. Here's how I'm doing it now:

#!/usr/bin/perl
##Restart NATD
print `kill \`cat /var/run/natd.pid\``;
sleep (20);
print `/sbin/natd -f /etc/natd.conf`;

I need to run a loop that continuously checks for natd to be killed before it tries to run again. Since I'm doing all this remotely it causes major problems if the timing is off and it tries to restart but can't. What would be ideal is if it even checks after the restart it checks one more time to see if it fails. If it fails it could start up with a default backup natd.conf.backup file.

#!/bin/ksh
kill \`cat /var/run/natd.pid\`
while true
do
if [ -z $(ps -ef |grep natd | grep -v grep) ] ; then
/sbin/natd -f /etc/natd.conf
fi
done
exit

Help completing this script would be great. Thanks in advance!
 
Old 03-25-2004, 12:28 PM   #2
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
ps aux | grep natd

if there is something wait till it is gone, of course make sure your not catching the "grep natd"

Last edited by Stack; 03-25-2004 at 12:29 PM.
 
Old 03-25-2004, 08:51 PM   #3
rspurlock
LQ Newbie
 
Registered: Jul 2003
Location: Knoxville, TN
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks for the Reply Stack but I'm not sure you read my entire post. I understand how to check to see if a process is running but I'm not at the server. If natd is killed, it will kick everyone off the server and the whole system will go down. On top of that, you must wait for natd to die before you can restart it. A simple kill then start back up script results in a dead server everytime. I need the script to kill it (easy enough) but then watch for it to die. Once it's dead, the script needs to restart it then end the script. That's more what I'm searching for. I have most of the script in my original post but I'm not adept at scripting and don't understand how to edit it to make it work.

Thanks for any comments!
 
Old 03-25-2004, 10:29 PM   #4
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
If you understand how to check a process is running on your computer how come you cant apply that to a remote machine? Have the script do a ps aux | grep natd and keep doing it every 2-3 seconds until it no longer returns a value for natd. Then restart it and end the script.
 
Old 03-27-2004, 01:52 AM   #5
rspurlock
LQ Newbie
 
Registered: Jul 2003
Location: Knoxville, TN
Posts: 13

Original Poster
Rep: Reputation: 0
If I kill natd remotely, I get disconnected. I need the script to do it for me so I don't have to. The server has a private IP address and is nat'd to it's public. So if I kill natd, I kill all connections.
 
Old 04-07-2004, 06:43 AM   #6
nejoom
LQ Newbie
 
Registered: Mar 2002
Location: Holland
Distribution: RH
Posts: 15

Rep: Reputation: 0
youll always lose the connection.
not much u can do about that unless there is a gracefull restart command.
but it should go back up.

The scripts are obviously on the remote machine.

chmod 755 testme

to make it execute

and youll need local access to at least test whats going on

------------------------testme--------------------------
#!/bin/bash
# filed named testme
process=`cat /var/run/natd.pid`
kill -9 $process

#kill the natd
while true
do
echo -e "waiting... \n";
if [ -z $(ps -ef |grep natd | grep -v grep) ] ; then
/sbin/natd -f /etc/natd.conf
fi
done

#wait for process to start
sleep(5);

#check it worked, if not use backup script
if [ -z $(ps -ef |grep natd | grep -v grep) ] ; then
echo -e "using backup...\n";
/sbin/natd -f /etc/natd.conf.backup
fi

exit

--------------------------------------------------------

#!/usr/bin/perl
##Restart NATD
print `\path\testme`;
 
Old 04-12-2004, 11:32 PM   #7
rspurlock
LQ Newbie
 
Registered: Jul 2003
Location: Knoxville, TN
Posts: 13

Original Poster
Rep: Reputation: 0
How I made it work

I took what you gave me and combined it with some other posts I dug through and came up with a slightly different version which I wanted to share. I couldn't quite get yours to work as provided but with the modifications it does the job.

#!/bin/sh
# filed named killbill
# set -x
process=`cat /var/run/natd.pid`
TEST=0
/usr/local/bin/sudo -u root /bin/kill -9 $process

# kill the natd
while [ $TEST = 0 ];
do
TEST=`ps ax | grep natd | grep -v grep | grep -v restart-natd | wc -l`
if [ $TEST = 0 ] ; then
/sbin/natd -f /etc/natd.conf
fi
done


exit 0


Questions is... How can I add a test within the if statement to check to see if it started correctly? The way you have it laid out, it checks after the while statement completes to see if it's running to start it with the backup config, but problem is that if the original conf file doesn't allow it to start correctly, it will just fail completely and be stuck in the loop. So I figure we could next the if statement that checks to make sure it restarts correctly within the original if statement. Something like this:

while [ $TEST = 0 ];
do
TEST=`ps ax | grep natd | grep -v grep | grep -v restart-natd | wc -l`
if [ $TEST = 0 ] ; then
/sbin/natd -f /etc/natd.conf;
TEST=`ps ax | grep natd | grep -v grep | grep -v restart-natd | wc -l`
if [ $TEST = 0 ] ; then
/sbin/natd -f /etc/natd.conf.backup;
fi
fi
done


Would this work as written? I'm not a scripting novice even so any help with the specifics would be appreciated.

Lastly, I call my shell script using a PHP file with a web browser. How can I have the shell script return data back to the PHP file to tell it that it did or didn't restart correctly?

TIA

Rob
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
How can I check to see if a script is running? slick_willie Programming 13 03-16-2007 10:18 AM
Scripts to check if a process is running kt8993 Programming 3 07-09-2005 05:32 PM
[script] check for a running process mikshaw Linux - Software 2 01-13-2004 08:33 PM
program to check to make sure a process is running? IceNineJon Linux - Software 7 08-06-2003 02:07 PM

LinuxQuestions.org > Forums > Other *NIX Forums > *BSD

All times are GMT -5. The time now is 01:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration