linux - 使用 start-stop-daemon 时,autossh pid 不等于 pidfile 中的 pid

标签 linux ssh debian autossh

我正在尝试在我的 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/

相关文章:

linux - 使用与主机上不同的用户启动 Docker 容器

php - 通过 cURL 获取 "card-title"html 元素并解析/输出响应

c++ - 我如何检测 CDRom 是否是 Linux 上的 DVD

安卓硬件调试

c - Malloc 没有失败,但系统内存不足

git - 通过openssh从git Windows服务器克隆

git - 是否可以在 git repo 上切换用户?

PHP shell_exec cp -r

ssh - 通过 SSH key 的服务器身份验证失败

rest - 迁移到 Parse-Server 后解析中的 REST-API 问题