ssh tunnel as a daemon [SOLVED]

Patrick Nelson pnelson at neatech.com
Sat Jun 12 20:36:36 UTC 2004


The following summarizes the way I have this working.  Note:  the -f to
the ssh command truly forks the process away from the script.  Which
isn't a huge deal but, I did have to add a killall ssh to get the init
scrip to stop both the script and the ssh tunnel.  Also in the init
script the daemon function didn't work so I had to background the
process.  Final result to the init script:

the init script (sshtund):

start call:
---snip---
 $ProgramFull &
---snip---

stop call:
---snip---
killproc $ProgramName
killall  $ProgramFork > /dev/null 2>&1
---snip---

the script loop:
---snip---
while [ true ]; do
 pidof ssh
 if [ $? -eq 1 ]; then
  ssh -fnN2g -c $Crypt $LPort:$LHost $RUser@$RHost -L $RPort:$LHost:$LPort
 fi
 sleep 10
done

this gives me quite a bit a flexibility and I feel it works well.  The
only problem I see is that it may be an issue if I had muliple ssh
tunnels working on the same system.  

On Sat, 2004-06-12 at 12:11, Patrick Nelson wrote:
> One way I tried is using pidof like this in the script sshtun:
> 
> ---snip---
> while [ true ]; do
>  pidof ssh
>  if [ $? -eq 1 ]; then
>   ssh -nN2g -c $Crypt $LPort:$LHost $RUser@$RHost -L $RPort:$LHost:$LPort
>  fi
>  sleep 5
> done
> ---snip---
> 
> which appears to work, recreating the ssh tunnel when it drops off. 
> Thoughts, questions, anecdotes?
> 
> 
> On Sat, 2004-06-12 at 11:47, Patrick Nelson wrote:
> > FC2 Uptodate
> > 
> > I'm trying to figure out how to daemon'ize a script that I have to start
> > an ssh port forwading tunnel.  Here is how I'm doing it:
> > 
> > the script (name is sshtun):
> > ---snip---
> > while [ true ]; do
> >  ssh -nN2g -c $Crypt $LPort:$LHost $RUser@$RHost -L $RPort:$LHost:$LPort
> > done
> > ---snip---
> > 
> > the init script (sshtund):
> > ---snip---
> > daemon $ProgramFull
> > ....
> > killproc $ProgramFull
> > ---snip---
> > 
> > The problem:
> > When I call service sshtund start, the ssh process does go to the
> > background and sshtund never exits.
> > 
> > So I tried this with the init:
> > ---snip---
> > $ProgramFull &
> > ....
> > killproc $ProgramFull
> > ---snip---
> > 
> > but then service sshtund stop doest work.  So I changed the stop section
> > of the init to:
> > 
> > ---snip---
> > $ProgramFull &
> > ....
> > killall -qgs 9 $ProgramName
> > ---snip---
> > 
> > where ProgramName is sshtun.  And this works but all further processing
> > of the init script (everything that follows the killall) doesn't get
> > done and the init script returns.
> > 
> > So, it appears that the ssh is holding the init script and tie'ing it to
> > the ssh command but I can not seem to separate the init script from the
> > program it calls (not sure if it is  possible).  OK thats cool, So I can
> > give the ssh command (in the script sshtun) the -f option to put ssh in
> > the background.  But I have to get rid of the while-do-done which
> > creates my real problem.  So, my question is:
> > 
> > Is there a best practice on scripting a process to make sure a
> > background process is running?  Or when a background process drops off
> > it triggers an event that would attempt to start it up again?  Any ideas
> > would help.
> > 
> 





More information about the fedora-list mailing list