autossh 的可靠 systemd 服务

gue*_*tli 5 ssh ssh-tunnel systemd

我尝试为 autossh 创建一个可靠的 systemd 服务。

服务正常工作,但如果主机密钥发生变化,则服务处于正常状态(正在运行)。

如果隧道不起作用,我希望它处于“失败”状态。

这是我当前的 systemd 服务文件:

# Source is in srv/salt/tunnel/autossh\@.service
# which is a git repo.
# Don't edit /etc/systemd/system/autossh\@.service  
[Unit]
Description=Tunnel For %i
After=network.target

[Service]
User=autossh
# https://serverfault.com/a/563401/90324
ExecStart=/usr/bin/autossh -M 0 -N -o "ExitOnForwardFailure yes" -o "ConnectTimeout=1" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R 40443:installserver:40443 -R 8080:installserver:8080 tunnel@%i
Restart=always

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

这是输出 systemctl status autossh@foo-work

salt:/srv # systemctl status autossh@foo-work
autossh@foo-work.service - Tunnel For foo-work
      Loaded: loaded (/etc/systemd/system/autossh@.service; enabled)
      Active: active (running) since Wed, 2016-02-10 14:35:01 CET; 2 months and 3 days ago
    Main PID: 17995 (autossh)
      CGroup: name=systemd:/system/autossh@.service/foo-work
          ? 17995 /usr/bin/autossh -M 0 -N -o ExitOnForwardFailure yes -o ConnectTimeout=1 -o ServerAliveInterval 60 -o ServerAliveCountMax 3 -R 40443:installserver:40443 -R ...

Apr 14 12:35:43 salt autossh[17995]: Host key verification failed.
Apr 14 12:35:43 salt autossh[17995]: ssh exited with error status 255; restarting ssh
Apr 14 12:45:42 salt autossh[17995]: starting ssh (count 618)
Apr 14 12:45:42 salt autossh[17995]: ssh child pid is 22524
Apr 14 12:45:43 salt autossh[17995]: Host key verification failed.
Apr 14 12:45:43 salt autossh[17995]: ssh exited with error status 255; restarting ssh
Run Code Online (Sandbox Code Playgroud)

我的问题不是更改的主机密钥。没关系。

我只是想让服务告诉我真相:如果隧道不工作,那么我希望它看到它。

如何更改 systemd 服务文件以告诉我正确的状态?

更新:我写了第二个后续问题:systemd 如何决定服务是否正常

Jak*_*uje 1

问题不是它失败了,而是它正在考虑服务处于活动状态,因为它将在接下来的 10 分钟内重新启动。

我没有尝试过,但它可能会起作用。尝试添加Type=forkingPIDFile

[Service]
...
Type=forking
Environment="AUTOSSH_PIDFILE=/path/to/pid"
PIDFile=/path/to/pid
Run Code Online (Sandbox Code Playgroud)

对于simple服务,systemd跟踪它们可能会遇到问题。