*BSDThis forum is for the discussion of all BSD variants.
FreeBSD, OpenBSD, NetBSD, etc.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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!
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.
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.
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.
#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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.