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



我已经为我的守护进程创建了一个RPM包,在安装时在/etc/init.d/下创建服务初始化脚本现在我想将这个RPM包移植到CentOS7.1,并使用systemd框架通过boot启动服务,以及通过admin通过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.
--
-- 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 init文件也能正常工作。

你无法在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

所以你可以使用所有的命令(chkconfig, service),因为你习惯从CentOS 6。当你有时间的时候,你可以学习man systemd.unit(5)和一堆其他手册页(参见该手册页的"see ALSO")。

最新更新