Thursday, January 24, 2013

Make shell script(PuTTY plink or SSH) a daemon that never dies! (using Ubuntu Upstart)

I have a script that creates a SSH Tunnel using PuTTY plink. Problem is that it dies sometimes. I have a old laptop running Ubuntu Server in my home. Also I want that script to be executed when the server starts up and automatically respawns another instance if it dies.

There are some old fashioned ways to do it which I won't detail. I'm going to introduce you the modern way using Upstart.

My first config is this one. Located at "/etc/init/plink.conf".

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

respawn

exec plink -D 7070 -N -pw mypassword user@host

However, this just won't work. I checked its status using "sudo status plink", only to find its state is "stopped/waiting". I also use "ps -ejH" to debug and find its pid changes every few seconds.Why is this happening?

Then I came across this page: http://www.greenend.org.uk/rjk/sshfwd/. It enlightens me to find out what plink says.

I created a script like this:

# redirect stderr to file 1
exec > /home/clear/ssh-forwarding.log 2>&1

exec plink -D 7070 -N -pw mypassword user@host
When I cat /home/clear/ssh-forwarding.log, then I found this:
The authenticity of host 'host (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is 62:af:59:cb:ef:89:b7:45:bc:56:a1:96:59:56:49:2f.
Are you sure you want to continue connecting (yes/no)? 

Then I realized that Upstart keeps respawning plink because it timeouts waiting for my answer.

The solution is simple.
1. su to root
2. issue the plink command and answer yes to trust the host.

The final plink.conf:

start on runlevel [2345] or net-device-up IFACE!=lo
stop on runlevel [!2345]
 
respawn
respawn limit 5 60 # respawn max 5 times in 60 seconds

script
 set -e
 exec > /home/clear/ssh-forwarding.log 2>&1
 exec plink -D 7070 -N -pw mypassword user@host
end script
Finally, reboot. I happily find that plink is running now!

No comments:

Post a Comment