将基本的 upstart 脚本迁移到 systemd

trv*_*vrm 17 upstart systemd

我刚刚将服务器从 Ubuntu 14.10 升级到 15.04,并且我通过自定义 upstart 脚本启动的一些服务不再运行。

我的理解是我需要将它们重新编写为systemd服务,但是systemd一夜之间学习整个系统的想法有点令人生畏。

upstart 脚本只是autossh在启动时启动,我还有其他几个类似的脚本可以启动长时间运行的进程。

#/etc/init/autossh.conf

description "Maintain a permanent SSH tunnel to <other_server>"

start on started mountall
stop on shutdown

exec autossh -N other_server
Run Code Online (Sandbox Code Playgroud)

如何将其重写为systemd服务?

Jde*_*eBP 13

迁移到 systemd 的第一条规则

在这一点上,在 2015 年,很可能已经有人这样做了。

systemd 已经存在好几年了。并且有一个完整的家庭手工业,人们编写单元文件并发布它们。尤其是 GitHub,似乎吸引了服务单元集合的存储库。

事实上,只需在 WWW 上搜索autossh.service(作为短语)就会出现:

模板单元

也就是说,正如我在 StackExchange 上的几个地方指出的那样,这种迁移不是一个机械过程,有时只是从任何一个单元文件自动转换为错误,或者至少很糟糕。在这种情况下,autossh肯定希望用模板单元处理,它被实例化为实际的服务单元,由目标名称参数化。因此/etc/systemd/system/autossh@.service,有:

[单元]
说明=用于来自 %i 的反向隧道的 AutoSSH 服务 
之后=网络.目标

[服务]
用户=autossh
EnvironmentFile=/etc/%p/%i.conf
ExecStart=/usr/bin/autossh -M 0 -q -N $SSH_USER@%i $SSH_OPTIONS

[安装]
WantedBy=multi-user.target

创建一个名为的文件/etc/autossh/other_server.example.conf,至少:

SSH_USER=乔

然后适用所有常用控件:

  • systemctl enable autossh@other_server.example — 使实例在引导时自动启动。
  • systemctl start autossh@other_server.example — 立即手动启动该实例。
  • systemctl status autossh@other_server.example — 查看它的状态。

是的,第一条规则甚至适用于此。通过搜索,人们会发现我被 OpenSUSE 的 Greg Freemyer 打败了不到两周。