简体   繁体   English

RPM SPEC Systemd启用和启动

[英]RPM SPEC Systemd enable and start

I have created a RPM SPEC file but I am struggeling with Systemd enabling and starting. 我已经创建了RPM SPEC文件,但是我在努力使Systemd启用和启动。 Updating the package through yum disables and stops the service. 通过yum更新软件包将禁用并停止该服务。 The distribution is Centos 7.x 发行版是Centos 7.x

I have installed the service under /etc/systemd/system. 我已经在/ etc / systemd / system下安装了该服务。 This is what I have tried so for but it does not work. 这是我尝试过的,但是没有用。

I have not found any good working examples of how to do this. 我还没有找到任何有效的方法来执行此操作的示例。

I used this page as reference. 我将此页面用作参考。 https://docs.fedoraproject.org/en-US/packaging-guidelines/Scriptlets/#_systemd https://docs.fedoraproject.org/en-US/packaging-guidelines/Scriptlets/#_syntax https://docs.fedoraproject.org/zh-CN/packaging-guidelines/Scriptlets/#_systemd https://docs.fedoraproject.org/zh-CN/packaging-guidelines/Scriptlets/#_syntax

    #Pre installation/upgrade of RPM section
    %pre      
      #Upgrading
      if [ $1 -eq 2 ]; then
        /usr/bin/systemctl stop %{pkgname}.service >/dev/null 2>&1 ||:
      fi

    %post
    %systemd_post %{pkgname}.service

      if [ $1 -eq 1 ]; then        
        /usr/bin/systemctl daemon-reload
        /usr/bin/systemctl start %{pkgname}.service
      fi
      if [ $1 -eq 2 ]; then
        /usr/bin/systemctl daemon-reload
        /usr/bin/systemctl start %{pkgname}.service    
      fi

   %preun
   %systemd_preun %{pkgname}.service
    #old package
    #uninstall
    if [ $1 -eq 0 ]; then
      /usr/bin/systemctl --no-reload disable %{pkgname}.service
      /usr/bin/systemctl stop %{pkgname}.service >/dev/null 2>&1 ||:
      /usr/bin/systemctl disable %{pkgname}.service

    fi
    if [ $1 -eq 1 ]; then
      /usr/bin/systemctl --no-reload disable %{pkgname}.service
      /usr/bin/systemctl stop %{pkgname}.service
    fi

1) the %{pkgname}.service should be placed in %{_unitdir} which expands to /usr/lib/systemd/system/ 1)应将%{pkgname}.service放在%{_unitdir} ,该文件应扩展到/usr/lib/systemd/system/

2) When you use %systemd_post %{pkgname}.service macro there is no need to have there: 2)当您使用%systemd_post %{pkgname}.service宏时,不需要在那里:

  if [ $1 -eq 1 ]; then        
    /usr/bin/systemctl daemon-reload
    /usr/bin/systemctl start %{pkgname}.service
  fi
  if [ $1 -eq 2 ]; then
    /usr/bin/systemctl daemon-reload
    /usr/bin/systemctl start %{pkgname}.service    
  fi

Similary the same for %pre and %preun . %pre%preun

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

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