如何将包含 SysV 初始化脚本的 RPM 包移植到 systemd?

How to port an RPM package consist of SysV init script to systemd?

我为我的守护进程创建了一个 RPM 包,它在安装时在 /etc/init.d/ 下创建服务初始化脚本 现在我想为 CentOS7.1 移植这个 RPM 包,并使用 systemd 框架通过引导以及管理员通过 start/stop 命令启动服务。

我找不到任何教程。请帮忙。

编辑: 我尝试了msuchy的建议。

我试过了,这是我的观察结果

基于 sysV 的框架 (CentOS6.5)

[root@adil work]# /etc/init.d/daemon_script status
Service daemon: Stopped
[root@adil work]# /etc/init.d/daemon_script start
Starting daemon: Initializing daemon... [ OK ]
[root@adil work]#
[root@adil work]# /etc/init.d/daemon_script status
Service daemon: Running
[root@adil work]#
[root@adil work]# /etc/init.d/daemon_script stop
Shutting down parent daemon: [ OK ]
[root@adil work]# /etc/init.d/daemon_script status
Service daemon: Stopped
[root@adil work]#

===========
在基于 systemd 的框架上,我在 CentOS7.1

上安装了相同的 RPM

[root@localhost x86_64]# /etc/init.d/daemon_script
Usage: /etc/init.d/daemon_script {start|stop|restart|status}
[root@localhost x86_64]# /etc/init.d/daemon_script start
Starting daemon_script (via systemctl): Warning: Unit file of daemon_script.service changed on disk, 'systemctl daemon-reload' recommended.
Job for daemon_script.service failed. See 'systemctl status daemon_script.service' and 'journalctl -xn' for details.
[FAILED]
[root@localhost x86_64]# systemctl daemon-reload
[root@localhost x86_64]# systemctl status daemon_script.service
daemon_script.service - SYSV: start and stop Test daemon service.
Loaded: loaded (/etc/rc.d/init.d/daemon_script)
Active: failed (Result: exit-code) since Fri 2015-09-11 15:30:44 IST; 32s ago

Sep 11 15:30:44 localhost.localdomain systemd[1]: Starting SYSV: start and st...
Sep 11 15:30:44 localhost.localdomain systemd[1]: daemon_script.service: cont...
Sep 11 15:30:44 localhost.localdomain systemd[1]: Failed to start SYSV: start...
Sep 11 15:30:44 localhost.localdomain systemd[1]: Unit daemon_script.service ...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost x86_64]# systemctl status daemon_script.service -l
daemon_script.service - SYSV: start and stop Test daemon service.
Loaded: loaded (/etc/rc.d/init.d/daemon_script)
Active: failed (Result: exit-code) since Fri 2015-09-11 15:30:44 IST; 46s ago

Sep 11 15:30:44 localhost.localdomain systemd[1]: Starting SYSV: start and stop Test daemon service....
Sep 11 15:30:44 localhost.localdomain systemd[1]: daemon_script.service: control process exited, code=exited status=203
Sep 11 15:30:44 localhost.localdomain systemd[1]: Failed to start SYSV: start and stop Test daemon service..
Sep 11 15:30:44 localhost.localdomain systemd[1]: Unit daemon_script.service entered failed state.
[root@localhost x86_64]#

journalctl -xn 的输出

-- Logs begin at Fri 2015-09-11 14:50:35 IST, end at Fri 2015-09-11 15:40:01 IST. --
Sep 11 15:31:03 localhost.localdomain systemd[1]: [/usr/lib/systemd/system/dm-event.socket:10] Unknown lvalue 'RemoveOnStop' in section 'Socket'
Sep 11 15:39:33 localhost.localdomain systemd[1]: Starting SYSV: start and stop Test daemon service....
-- Subject: Unit daemon_script.service has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit daemon_script.service has begun starting up.
Sep 11 15:39:33 localhost.localdomain systemd[8509]: Failed at step EXECspawning /etc/rc.d/init.d/daemon_script: Exec format error
-- Subject: Process /etc/rc.d/init.d/daemon_script could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /etc/rc.d/init.d/daemon_script could not be executed and failed.
--
-- The error number returned while executing this process is 8.
Sep 11 15:39:33 localhost.localdomain systemd[1]: daemon_script.service: control process exited, code=exited status=203
Sep 11 15:39:33 localhost.localdomain systemd[1]: Failed to start SYSV: start and stop Test daemon service..
-- Subject: Unit daemon_script.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit daemon_script.service has failed.
--
-- The result is failed.
Sep 11 15:39:33 localhost.localdomain systemd[1]: Unit daemon_script.service entered failed state.
Sep 11 15:40:01 localhost.localdomain systemd[1]: Created slice user-0.slice.
-- Subject: Unit user-0.slice has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit user-0.slice has finished starting up.
--
-- The start-up result is done.
Sep 11 15:40:01 localhost.localdomain systemd[1]: Starting Session 7 of user root.
-- Subject: Unit session-7.scope has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-7.scope has begun starting up.
Sep 11 15:40:01 localhost.localdomain systemd[1]: Started Session 7 of user root.
-- Subject: Unit session-7.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-7.scope has finished starting up.
--
-- The start-up result is done.
Sep 11 15:40:01 localhost.localdomain CROND[8528]: (root) CMD (/usr/lib64/sa/sa1 1 1)
[root@localhost x86_64]#

您不需要迁移脚本。

它可能会更好,您可以利用 Systemd 的一些有趣的功能,您的文件会更小。但是您不需要 迁移。 Systemd 也可以与 SysV 初始化文件一起正常工作。

您无法在 CentOS 7 安装上找到任何 SysV init 文件,因为 Red Hat 打包程序(创建 CentOS)努力打包并将所有 SysV 文件迁移到单元文件。但你不需要。

这只是您需要完成的一项任务。放置新的 SysV 文件后,您必须使用

重新加载 systemd 管理器配置
# systemctl daemon-reload

就是这样。 我举个小例子

# cat /etc/init.d/foo
#!/usr/bin/sh
echo ahoy

# chmod a+x /etc/init.d/foo
# systemctl start foo
Failed to start foo.service: Unit foo.service failed to load: No such file or directory.
# systemctl daemon-reload
# systemctl start foo
# journalctl -xn
-- Subject: Unit foo.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit foo.service has finished starting up.
-- 
-- The start-up result is done.
# service foo start
ahoy

因此您可以像从 CentOS 6 开始使用的所有命令(chkconfig、服务)。当您有时间时,您可以研究 man systemd.unit(5) 和许多其他手册页(请参阅"SEE ALSO" 该手册页)。