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! |
ps aux | grep natd
if there is something wait till it is gone, of course make sure your not catching the "grep natd" :) |
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! |
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.
|
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`; |
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 |
All times are GMT -5. The time now is 03:34 AM. |