I am trying to create a service on my Debian Wheezy system.
When trying to use start-stop-daemon to run autossh, the pid contained into the pidfile does not match with the autossh process.
$ AUTOSSH_PIDFILE=/var/run/padstunnel.pid
$ sudo start-stop-daemon --make-pidfile --background --name mytunnel --start --pidfile /var/run/mytunnel.pid --exec /usr/lib/autossh/autossh -- -M 0 -p 22 user@server -f -T -N -R 31022:localhost:31222
$ ps -elf |grep autossh
1 S root 447 1 0 80 0 - 329 pause 19:07 ? 00:00:00 /usr/lib/autossh/autossh -M 0 -p 22 ...
$ cat /var/run/mytunnel.pid
446
This behaviour prevents from stopping autossh using start-stop-daemon or kill it using the pid from the pidfile.
Is there any reason to this behaviour ?
How to workaround it and make autossh's pid matches the pidfile ?
The solution I found is inspired by this answer.
Actually, the
AUTOSSH_PIDFILEvariable could not be used byautossh(because start-stop-daemon runs in a different environment).So the workaround is to use :
/usr/bin/env AUTOSSH_PIDFILE="/var/run/mytunnel.pid"correctly defines the necessary environment variable--make-pidfileand--pidfileare no longer required bystart-stop-daemonsudo start-stop-daemon --pidfile /var/run/mytunnel.pid --stopnow works to killautossh--backgroundoption makes thessh's-foptional (using-for not does not change anything if--backgroundis used)The reason for the behaviour is not completely clear to me. However, it seems that
autosshautomatically creates several processes to handle correctlysshinstances when it does not seeAUTOSSH_PIDFILEvariable.Edit:
When using it from a service init script (in /etc/init.d/servicename), the syntax has to be modified:
Notice the
--that must come just after the /usr/bin/env command (it was after the /usr/lib/autossh/autossh from command line).