将基本的upstart脚本迁移到systemd
我刚刚将服务器从Ubuntu 14.10升级到15.04,并且我通过自定义upstart脚本启动的一些服务不再运行。
我的理解是我需要将它们重新编写为systemd
服务,但是一夜之间学习整个systemd
系统的想法有点令人生畏。
upstart脚本只是在启动时启动autossh
,我还有一些其他类似的脚本可以启动长时间运行的进程。
#/etc/init/autossh.conf description "Maintain a permanent SSH tunnel to " start on started mountall stop on shutdown exec autossh -N other_server
如何将其重新编写为systemd
服务?
迁移到systemd的第一条规则
在这一点上,在2015年,很可能有人已经完成了它。
systemd已存在多年。 整个家庭手工业都有人写单元文件并发布它们。 特别是GitHub似乎吸引了服务单元集合的存储库。
事实上,只需在WWW上搜索autossh.service
(作为短语)就会出现:
- Phillipp Klaus在2013年这样做
- Thomaz Fritz在2014年这样做
- 不知名的人在2013年这样做
模板单元
也就是说,正如我在StackExchange上的几个地方所指出的那样,这种迁移不是一个机械过程,有时只是机器人从一个单元文件转换到一个单元文件是错误的,或者至少是糟糕的。 在这种情况下, autossh
积极地用模板单元处理,它被实例化为实际的服务单元,由目标名称参数化。 所以/etc/systemd/system/autossh@.service
,有:
[单元] 描述=来自%i的反向隧道的AutoSSH服务 之后= network.target [服务] 用户= 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
– 启用在bootstrap时自动启动的实例。 -
systemctl start autossh@other_server.example
– 立即手动启动该实例。 -
systemctl status autossh@other_server.example
– 查看其状态。
是的,第一条规则甚至适用于此。 通过搜索,人们可以发现我在OpenSUSE的Greg Freemyer两周后遭到了殴打。