我正在尝试在我的 Debian Wheezy 系统上创建一个服务。
当尝试使用 start-stop-daemon 运行 autossh 时,pid 文件中包含的 pid 与 autossh 进程不匹配。
$ 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
此行为可防止使用 start-stop-daemon 停止 autossh
或使用 pid 文件中的 pid 终止它。
这种行为有什么原因吗?
如何解决它并使 autossh 的 pid 与 pid 文件匹配?
最佳答案
我找到的解决方案的灵感来自 this answer .
实际上,AUTOSSH_PIDFILE
变量不能被 autossh
使用(因为 start-stop-daemon 运行在不同的环境中)。
所以解决方法是使用:
$ sudo start-stop-daemon --background --name mytunnel --start --exec /usr/bin/env AUTOSSH_PIDFILE="/var/run/mytunnel.pid" /usr/lib/autossh/autossh -- -M 0 -p 22 user@server -f -T -N -R 31022:localhost:31222
/usr/bin/env AUTOSSH_PIDFILE="/var/run/mytunnel.pid"
正确定义必要的环境变量--make-pidfile
和--pidfile
不再被start-stop-daemon
所需要sudo start-stop-daemon --pidfile/var/run/mytunnel.pid --stop
现在可以杀死autossh
--background
选项使ssh
的-f
可选(使用或不使用-f
如果使用--background
,则不要更改任何内容)
我不完全清楚这种行为的原因。但是,似乎 autossh
在没有看到 AUTOSSH_PIDFILE
变量时会自动创建几个进程来正确处理 ssh
实例。
编辑:
从服务初始化脚本(在/etc/init.d/servicename 中)使用它时,必须修改语法:
sudo start-stop-daemon --background --name mytunnel --start --exec /usr/bin/env -- AUTOSSH_PIDFILE="/var/run/mytunnel.pid" /usr/lib/autossh/autossh -M 0 -p 22 user@server -f -T -N -R 31022:localhost:31222
注意 --
必须紧跟在/usr/bin/env 命令之后(它在命令行的/usr/lib/autossh/autossh 之后)。
关于linux - 使用 start-stop-daemon 时,autossh pid 不等于 pidfile 中的 pid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34094792/