Start call-home SSH reverse tunnel on Debian reboot

listed in answer

Start call-home SSH reverse tunnel on Debian reboot
0 votes, 0.00 avg. rating (0% score)

ANSWER:

It used to be the case that you could put a call to each of those scripts in an inittab entry, and be done with it. Simple. But ever since Debian switched to using upstart instead of init, things have gotten more complicated and fragile. You’ll need to write an upstart configuration file; those go in /etc/init, and they’re easier to get wrong.

What you might do is grep through the contents of /etc/init/* looking for existing configs which use the ‘respawn’ keyword, and that start late enough that the network is already up. Copy and modify one of those for each of your scripts. The convention is to put your own scripts in /etc/init/local/*.

You might be able to remove some of the fragility by starting the scripts earlier during boot, and just letting them hang, die, timeout, or whatever when the network isn’t up yet. The ‘respawn’ will eventually respawn them, possibly after a longer timeout if they retried too many times earlier.

By the way, you probably don’t need those ‘while true’ scripts. I usually use ‘autossh’ to start and manage these sorts of tunnels. It tends to be more reliable than a typical script, and the autossh command can be put directly in an /etc/init configuration file.

I hesitate to give you an example configuration file, because upstart configurations can be different enough that what works for me might not work for you (more of that fragility). But here is one of my autossh configs — this might go in, say, /etc/init/local/autossh-example.conf. You’ll want to test this locally on an identical machine before trusting it on your remote machine:

start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]

respawn
exec /usr/bin/autossh -R 20042:127.0.0.1:22 -N foo.example.com

by stevegt from http://serverfault.com/questions/384096