如何将包含 SysV 初始化脚本的 RPM 包移植到 systemd?
Posted
技术标签:
【中文标题】如何将包含 SysV 初始化脚本的 RPM 包移植到 systemd?【英文标题】:How to port an RPM package consist of SysV init script to systemd? 【发布时间】:2015-09-07 05:55:03 【问题描述】:我为我的守护进程创建了一个 RPM 包,它在安装时在 /etc/init.d/ 下创建服务初始化脚本 现在我想为 CentOS7.1 移植这个 RPM 包,并使用 systemd 框架通过 boot 以及管理员通过 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 EXEC
spawning /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.
@987654394 @-- 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]#
【问题讨论】:
也许这会有用? 0pointer.de/blog/projects/systemd-for-admins-3.html 即使您的服务位于 /etc/init.d/ 下,您也可以使用 systemd。只是单元文件为你提供了更多的选择更容易编写。 @msuchy 怎么样?我不是很清楚。在 centOS7 中,我看不到 /etc/init.d/ 目录中的任何条目 你看过我在your other similar question上给你的资源了吗? 您很可能在初始化脚本的第一行缺少 shebang ("#!/usr/bin/bash/")。 【参考方案1】:您不需要迁移脚本。
这可能会更好,您可以利用 Systemd 的一些有趣功能,您的文件会更小。但是您确实不需要迁移。 Systemd 也可以正确处理 SysV 初始化文件。
您在 CentOS 7 安装中找不到任何 SysV 初始化文件,因为 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、service)。当你有时间的时候,你可以学习man systemd.unit
(5) 和一堆其他的手册页(参见“另见”手册页)。
【讨论】:
以上是关于如何将包含 SysV 初始化脚本的 RPM 包移植到 systemd?的主要内容,如果未能解决你的问题,请参考以下文章