用于N个autossh隧道的守护进程

我使用autossh来创build从本地主机到远程主机的永久隧道。

手动启动autossh可以正常工作,但是如果localhost重新启动,隧道就会消失。 这并不奇怪。

如何创build一个守护进程重新启动本地主机后重新打开约20隧道?

服务器:支持systemd的Linux

将通常运行的任何命令添加到/etc/rc.local ,如果是则将一个&放在最后。

我们使用monit来本地监视/pipe理服务器上的各种进程。 要使用monitpipe理autossh实例,请安装monit软件包并创build一个configuration文件/etc/monit/conf.d/autossh.conf

 check process autossh1 pidfile "/tmp/autossh1.pid" start program = "/bin/bash -c 'export AUTOSSH_PIDFILE=/tmp/autossh1.pid; autossh -f [email protected]'" as uid user1 and group group1 stop program = "/bin/bash -c 'kill `cat /tmp/autossh1.pid`'" group autossh 

然后用sudo service monit restart

如果要以root用户身份运行进程,则可以在start program =行末尾省略as uid user1 and group group1

monit将定期检查进程是否正在运行,并在需要时重启。 您可以显示由monitpipe理的进程的状态:

 monit summary 

您也可以通过运行轻松停止/启动进程

 monit stop autossh1 monit start autossh1 

您甚至可以创build组(如线条group autossh ),然后停止/启动整个组:

 monit stop -g autossh monit start -g autossh 

顺便说一下, monit命令行工具使用HTTP与守护进程进行通信。 要使通信正常工作,您需要在/etc/monit/monitrc包含以下/etc/monit/monitrc

 set httpd port 2812 and use address localhost # only accept connection from localhost allow localhost # allow localhost to connect to the server 

希望有所帮助。

我发现一个systemd文件来启动一个autossh守护进程。 在我的情况下,我需要创buildN个这样的文件:

 [Unit] Description=Keeps a tunnel to 'remote.example.com' open After=network.target [Service] User=autossh # -p [PORT] # -l [user] # -M 0 --> no monitoring # -N Just open the connection and do nothing (not interactive) # LOCALPORT:IP_ON_EXAMPLE_COM:PORT_ON_EXAMPLE_COM ExecStart=/usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -p 22 -l autossh remote.example.com -L 7474:127.0.0.1:7474 -i /home/autossh/.ssh/id_rsa [Install] WantedBy=multi-user.target 

来源: https : //gist.github.com/thomasfr/9707568