I have an upstart service defined as:

/etc/init/sshproxy.conf

description    "Lenik's secret tunnel thru *.ssh.myserver.com"
author        "谢继雷 (Lenik)"

start on (net-device-up IFACE!=lo)
stop on runlevel[!2345]

script

    # -T disable pseudo-tty allocation
    # -f go to background after login but before command exec
    # -n stdin from /dev/null, must be used when ssh is run in bg.
    # -N no command
    # -D "dynamic" app-level port forwarding.
    sudo -usshproxy ssh -qTfn -ND *:7878 ssh.myserver.com

end script

But the ssh tunnel seems to be zombied after some hours, so I want to restart it each hour, how to do it within this .conf file, or should I write another cron.hourly job?

link|improve this question

feedback

2 Answers

up vote 2 down vote accepted

Rather than restart the service at fixed intervals, you could restart the service when the connection dies. Pass the ServerAliveInterval option to make ssh detect a broken connection faster. Use the respawn directive in the Upstart script to have the tunnel restarted automatically. Alternatively, use an ssh tunnel restarting program such as autossh.

link|improve this answer
Great information! – Xie Jilei Nov 26 '10 at 1:59
+1. Heck, I think that this answer should have been accepted instead. – whitequark Nov 26 '10 at 5:40
feedback

While "events generated at timed intervals" is a planned feature in Upstart, it is not ready to provide that yet. So you should write a simple script like this:

#!/bin/sh
service sshproxy restart 

And put it into /etc/cron.hourly folder. Don't forget to set an executable bit (chmod +x /etc/cron.hourly/whatever.sh), or it will not start.

link|improve this answer
Do user cron scripts are run in sudo mode? – Xie Jilei Nov 25 '10 at 2:49
No, they are run as the user they belong to, i.e. like if you'd call them manually from terminal when logged in as that user. – whitequark Nov 25 '10 at 2:58
Like most of Upstart, it is a planned feature. It's a really cool idea, but not actually implemented yet. – Tim Williscroft Nov 26 '10 at 1:08
@Tim, Upstart is currently replaces init nicely, and all of its planned features are, actually, replacing other Unix daemons. – whitequark Nov 26 '10 at 5:39
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.