ssh tunnel as a daemon
Patrick Nelson
pnelson at neatech.com
Sat Jun 12 19:11:44 UTC 2004
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