可靠的autossh系统服务

我尝试为autossh创build一个可靠的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 

这里是systemctl status autossh@foo-work的输出

 salt:/srv # systemctl status autossh@foo-work [email protected] - Tunnel For foo-work Loaded: loaded (/etc/systemd/system/[email protected]; 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/[email protected]/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 

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

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

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

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

问题不是它失败了,而是它正在考虑服务活动,因为它将在未来10分钟内重新启动。

我没有尝试,但它可能工作。 尝试添加Type=forkingPIDFile

 [Service] Type=forking Envinment="AUTOSSH_PIDFILE=/path/to/pid" PIDFile=/path/to/pid 

通过simple服务, systemd可能会有问题来跟踪它们。