简体   繁体   English

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

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

I have created an RPM package for my daemon which on installation create service init script under /etc/init.d/ Now I wanted to port this RPM package for CentOS7.1 and use systemd framework for service startup through boot as well as by admin through start/stop command. 我已经为守护程序创建了一个RPM软件包,该软件包在安装时在/etc/init.d/下创建了服务初始化脚本,现在我想将该CPMOS7.1移植到RPM软件包中,并使用systemd框架通过引导和管理来启动服务。通过启动/停止命令。

I couldn't find any tutorial. 我找不到任何教程。 Please help. 请帮忙。

EDIT: I tried the suggestion given by msuchy. 编辑:我尝试了msuchy给出的建议。

I tried and here is my observation 我尝试过,这是我的观察

On sysV based framework (CentOS6.5) 基于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]#

=========== ===========
On systemd based framework, I install the same RPM on CentOS7.1 在基于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]#

output of journalctl -xn 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 Sep 11 15:39:33 localhost.localdomain systemd[8509]: Failed at step EXEC spawning /etc/rc.d/init.d/daemon_script: Exec format error 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]#

You do not need to migrate your scripts. 您不需要迁移脚本。

It is probably better and you can utilize some interesting functions of Systemd, your files will be smaller. 可能会更好,并且您可以利用Systemd的一些有趣功能,您的文件会更小。 But you do not need to migrate. 但你并不需要迁移。 Systemd works correctly with SysV init files too. Systemd也可以与SysV初始化文件一起正常工作。

You can not locate any SysV init files on CentOS 7 installation because Red Hat packagers (who created CentOS) put an effort into packaging and migrated all SysV files to unit files. 您无法在CentOS 7安装上找到任何SysV初始化文件,因为Red Hat打包程序(创建了CentOS)致力于打包并将所有SysV文件迁移到单元文件。 But you do not need to. 但是您不需要。

The is only one task you need to do. 这只是您需要执行的一项任务。 Once you place new SysV file, you must reload the systemd manager configuration using 放置新的SysV文件后,必须使用以下命令重新加载systemd管理器配置

# systemctl daemon-reload

That is all. 就这些。 I give you small example 我举一个小例子

# 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

So you can use all command (chkconfig, service) as you are used to from CentOS 6. And when you have time, you can study man systemd.unit (5) and bunch of other man pages (see "SEE ALSO" of that man page). 因此,您可以像在CentOS 6上一样使用所有命令(chkconfig,service)。如果有时间,您可以学习man systemd.unit (5)和其他手册页(请参阅该手册的“另请参阅”)。手册页)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM