我发现这个systemd服务文件启动autossh以保持一个SSH隧道: https : //gist.github.com/thomasfr/9707568
[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
有没有办法configurationsystemd在一个服务中启动多个隧道。
我不想创buildN系统服务文件,因为我想避免复制+粘贴。
除“remote.example.com”将被replace为其他主机名称外,所有的服务文件都是相同的。
我大概在1.5年前问过这个问题。
我的思想已经改变了一点。 是的,这很好,你可以用systemd来做(我仍然使用它),但我将来会使用configurationpipe理。
systemd为什么要实现一个模板语言并replace%h?
几个月后,我认为这个循环和模板应该用自动configuration的工具解决。 我现在在维基百科上使用这个列表的一个工具。
那么,假设每个单元文件唯一更改的是remote.example.com部分,则可以使用实例化 服务 。
从systemd.unit手册页:
可选地,单元可以在运行时从模板文件实例化。 这允许从单个configuration文件创build多个单元。 如果systemd查找单元configuration文件,它将首先在文件系统中search文本单元名称。 如果没有成功,并且单元名称包含“@”字符,则systemd将查找共享相同名称但具有实例string(即“@”字符和后缀之间的部分)的单元模板。 例如:如果请求服务[email protected]并且没有find该名称的文件,systemd将查找getty @ .service,并在find该实例时实例化该configuration文件中的服务。
基本上,你创build一个单元文件,其中包含一个variables(通常是%i ),其中的差异发生,然后当你“启用”该服务时,他们得到链接。
例如,我有一个名为/etc/systemd/system/[email protected]的单元文件,如下所示:
[Unit] Description=AutoSSH service for ServiceABC on %i After=network.target [Service] Environment=AUTOSSH_GATETIME=30 AUTOSSH_LOGFILE=/var/log/autossh/%i.log AUTOSSH_PIDFILE=/var/run/autossh.%i.pid PIDFile=/var/run/autossh.%i.pid #Type=forking ExecStart=/usr/bin/autossh -M 40000 -NR 5000:127.0.0.1:5000 -i /opt/ServiceABC/.ssh/id_rsa_ServiceABC -l ServiceABC %i [Install] WantedBy=multi-user.target
我已经启用了
[user@anotherhost ~]$ sudo systemctl enable [email protected] ln -s '/etc/systemd/system/[email protected]' '/etc/systemd/system/multi-user.target.wants/[email protected]'
并可以相互作用
[user@anotherhost ~]$ sudo systemctl start [email protected] [user@anotherhost ~]$ sudo systemctl status [email protected] [email protected] - AutoSSH service for ServiceABC on somehost.example Loaded: loaded (/etc/systemd/system/[email protected]; enabled) Active: active (running) since Tue 2015-10-20 13:19:01 EDT; 17s ago Main PID: 32524 (autossh) CGroup: /system.slice/system-autossh.slice/[email protected] ├─32524 /usr/bin/autossh -M 40000 -NR 5000:127.0.0.1:5000 -i /opt/ServiceABC/.ssh/id_rsa_ServiceABC -l ServiceABC somehost.example.com └─32525 /usr/bin/ssh -L 40000:127.0.0.1:40000 -R 40000:127.0.0.1:40001 -NR 5000:127.0.0.1:5000 -i /opt/ServiceABC/.ssh/id_rsa_ServiceABC -l ServiceABC somehost.example.com Oct 20 13:19:01 anotherhost.example.com systemd[1]: Started AutoSSH service for ServiceABC on somehost.example.com. [user@anotherhost ~]$ sudo systemctl status [email protected] [user@anotherhost ~]$ sudo systemctl status [email protected] [email protected] - AutoSSH service for ServiceABC on somehost.example.com Loaded: loaded (/etc/systemd/system/[email protected]; enabled) Active: inactive (dead) since Tue 2015-10-20 13:24:10 EDT; 2s ago Process: 32524 ExecStart=/usr/bin/autossh -M 40000 -NR 5000:127.0.0.1:5000 -i /opt/ServiceABC/.ssh/id_rsa_ServiceABC -l ServiceABC %i (code=exited, status=0/SUCCESS) Main PID: 32524 (code=exited, status=0/SUCCESS) Oct 20 13:19:01 anotherhost.example.com systemd[1]: Started AutoSSH service for ServiceABC on somehost.example.com. Oct 20 13:24:10 anotherhost.example.com systemd[1]: Stopping AutoSSH service for ServiceABC on somehost.example.com... Oct 20 13:24:10 anotherhost.example.com systemd[1]: Stopped AutoSSH service for ServiceABC on somehost.example.com.
正如你所看到的,单元文件中%i所有实例都被somehost.example.comreplace。
虽然有更多的说明符可以在单元文件中使用,但是我发现%i在这种情况下工作得最好。
格雷格的回答帮了我很多。 下面是我在代码中使用的一个单元模板的例子,上面的例子是一个齿轮工作服务器。 我制作了一个shell脚本,可以让我使用这个模板创buildX个“工作人员”。
[Unit] Description=az gearman worker After=gearman-job-server.service [Service] PIDFile=/var/run/gearman_worker_az%i.pid Type=simple User=www-data WorkingDirectory=/var/www/mysite.com/jobs/ ExecStart=/usr/bin/php -f gearman_worker_az.php > /dev/null 2>&1 Restart=on-success KillMode=process [Install] WantedBy=multi-user.target
这是一个python的例子,这正是我正在寻找的:
/etc/systemd/system/[email protected]
[Unit] Description=manages my worker service, instance %i After=multi-user.target [Service] PermissionsStartOnly=true Type=idle User=root ExecStart=/usr/local/virtualenvs/bin/python /path/to/my/script.py Restart=always TimeoutStartSec=10 RestartSec=10
禁用30名员工: sudo systemctl enable my-worker\@{1..30}.service
启用30名员工: sudo systemctl enable my-worker\@{1..2}.service
重新加载: sudo systemctl daemon-reload
开始1: sudo systemctl start [email protected]
启动多个: sudo systemctl start my-worker@{1..2}
停止多个: sudo systemctl stop my-worker@{1..2}
检查状态: sudo systemctl status my-worker@1
希望它有帮助,干杯!