Linux – How to restart a service each hour, in upstart

debianredhat-enterprise-linuxssh-tunnelupstart

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?

Best Answer

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.