1

I am trying to make reverse ssh tunnels to my clients using upstart. I have many clients and they obviously come on and off line at times. This is what I have now, but it won't work because the env stanza does not support running the command.

description "Upstart Script to run sonarr as a service on Ubuntu/Debian based systems"
start on runlevel [2345]
stop on runlevel [016]
env PORT=$(curl http://ipvr.japayton.com/port.php)
env API=(sudo cat /home/kodi/IPVR/api)
respawn
exec autossh -nNT -i /home/root/.ssh/sshkeys -o ServerAliveInterval=15 -R $PORT:localhost:443 tunnel@ipvr.japayton.com
post-start exec ssh tunnel@ipvr.japayton.com 'sqlite3 /home/client/clients.db "UPDATE clients SET port = '$PORT' WHERE api = $API;"'

The API and post-start are not that important. I can put this into an external script since I think I am going to build an API for these updates rather than using ssh. The port.php returns an available port for the reverse tunnel.

I need that port at the time of the execution of autossh. I know I could make a script stanza and set it like a regular variable but the upstart would monitor the PID of curl instead of autossh. I don't know if using an external bash script would work. I want to avoid writing anything to the file system.

0

I don't think so. It sounds like most of this could (or should) be broken out into its own script so your upstart looks like:

description "Upstart Script to run sonarr as a service on Ubuntu/Debian based systems"
start on runlevel [2345]
stop on runlevel [016]

exec /usr/local/sbin/sonarr

And /usr/local/sbin/sonarr being:

#!/bin/sh
PORT=$(curl http://ipvr.japayton.com/port.php)
API=$(cat /home/kodi/IPVR/api)

# Do this first because it's non-blocking
# And take care with quotes.
ssh tunnel@ipvr.japayton.com 'sqlite3 /home/client/clients.db "UPDATE clients SET port = '"'$PORT'"' WHERE api = '"'$API'"';"'

autossh -nNT -i /home/root/.ssh/sshkeys -o ServerAliveInterval=15 -R $PORT:localhost:443 tunnel@ipvr.japayton.com

Note that there were a few syntax issues with the script so make sure you get that running before you start blaming Upstart for not launching it.

  • I would keep the ssh as it is because ordering is important. Also, he should exec the autossh in the script there. – CameronNemo Jan 14 '15 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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