LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-24-2007, 01:19 PM   #1
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Rep: Reputation: 30
Keep an exec running?


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.
 
Old 08-24-2007, 01:28 PM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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..
 
Old 08-24-2007, 01:30 PM   #3
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
This is what I have:
Code:
#! /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.
 
Old 08-24-2007, 01:48 PM   #4
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,340

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by Zeno McDohl View Post
This is what I have:
Code:
#! /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?

--------------
Steve Stites
 
Old 08-24-2007, 01:56 PM   #5
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
The exec uses port 54230, so I check if that port is in use.

LISTEN is used to look for that in netstat.
 
Old 08-26-2007, 10:08 AM   #6
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
Anyone have any suggestions?
 
Old 08-26-2007, 10:54 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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):
Code:
#!/bin/csh -f
[ ! -f ./server ] && echo read > server; chmod 0750 server
[ ! -x ./server ] && exit 127
set port = 54230
netstat -an | grep -q ":${port}.*LISTEN"
[ $? -eq 1 ] && exit 1 || ./server
exit 0
HTH
 
Old 08-26-2007, 01:10 PM   #8
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
Quote:
so you wouldn't want to keep the script alive anyway
You lost me. If the script isn't running, how is it suppose to boot the server back up when it dies?
 
Old 08-26-2007, 05:46 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
What exactly is this "server" if you don't mind me asking?
 
Old 08-26-2007, 06:52 PM   #10
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
It's a MUD.
 
Old 08-27-2007, 01:45 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 08-27-2007, 01:40 PM   #12
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
I know that. It dumps cores when it crashes. I gdb those and fix the bugs. But I can't predict the future and know when it'll crash.

So again. I'd like to figure out how to write a script to keep it running. And I don't think it writes the PID anywhere.
 
Old 08-27-2007, 04:03 PM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Code:
#!/bin/bash --
runServer() { ./server || runServer; }; netstat -ntl \
| grep -q ":54230[[:blank:]]" || runServer; exit 0
 
Old 08-27-2007, 06:53 PM   #14
nan0meter
Member
 
Registered: Aug 2007
Location: The Netherlands
Distribution: Fedora 7 x86_64
Posts: 119

Rep: Reputation: 15
Well doing this would consume quite an ammount of memory and CPU power wouldn't it? Can't you better poll for it with a timeout?
 
  


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
find the exact location of a running exec file cahdecah General 4 08-16-2007 10:53 AM
exec {} \; ddaas Linux - General 3 02-05-2007 09:02 AM
Running PHP exec() as root Calltor Programming 2 06-05-2006 04:04 AM
exec cmd=perl... work but exec cgi doenst crions Slackware 5 12-09-2005 12:17 PM
cannot exec as person Linux From Scratch 5 12-19-2003 04:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:56 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