Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

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.

share|improve this answer
    
Great information! –  Xiè Jìléi 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
add comment

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.

share|improve this answer
    
Do user cron scripts are run in sudo mode? –  Xiè Jìléi 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
    
You can also do put initctl emit a_custom_event in cron, and add or a_custom_event to the start on line in the upstart job –  nafg Nov 27 '13 at 11:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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