Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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 looking to write a script (csh perhaps) that keeps an exec running. So when it dies, the script will boot it back up. I've tried a few things, with no luck.
What have you tried? Any examples? What didn't work in them, what was the actual problem?
People here will more likely help you if you have actual problems to show, to show that you've already tried something. If you just come here and say "hey guys, I need a complete firewalling-and-security-hardening plan for a 1000 machine corporation, and I need it in two hours!" you'll probably get zero answers.
You have several options, one being to check if the process ID exists (and corresponds to the process name), and if not, launch the program and save it's new ID etc..
#! /bin/csh -f
set port = 54230
while ( 1 )
# Run Server.
# Check if already running
set matches = `netstat -an | grep ":$port " | grep -c LISTEN`
if ( $matches >= 1 ) then
# Already running
echo Port $port is already in use.
exit 0
endif
./server
end
I would expect the script to always be running, but it ends right after being run.
#! /bin/csh -f
set port = 54230
while ( 1 )
# Run Server.
# Check if already running
set matches = `netstat -an | grep ":$port " | grep -c LISTEN`
if ( $matches >= 1 ) then
# Already running
echo Port $port is already in use.
exit 0
endif
./server
end
I would expect the script to always be running, but it ends right after being run.
$matches will always be >= 1 so you will always take exit 0 which is what you report always happens. What condition(s) are you trying to check? Netscape running? Port 54230 in use? What is LISTEN?
A loop wouldn't make sense IMHO because it would keep executing instances of server. And usually a "server" implies something that forks itself off into the background so you wouldn't want to keep the script alive anyway. You also don't need two 'grep' instances and you don't need to "see" anything: just use the exit status. Exit status of a script on errorless completion should AFAIK be zero and any other values to denote errors. If the "server" doesn't background itself this is what happens (run with 'csh -x' to see what I mean):
how is it suppose to boot the server back up when it dies?
And why would it die? Wouldn't it be "better" to first find out *why* it dies and work on that before trying to implement a workaround? BTW, if it writes a PID somewhere and can use an initscript it would be easier to run a "real" process checker like Monit.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.