RPM SPEC Systemd 启用并启动

use*_*363 1 linux rpm centos7

我已经创建了一个 RPM SPEC 文件,但我在启用和启动 Systemd 方面遇到了困难。通过 yum 更新软件包会禁用并停止该服务。发行版是Centos 7.x

我已将该服务安装在/etc/systemd/system下。这就是我尝试过的目的,但它不起作用。

我还没有找到任何关于如何做到这一点的好的工作示例。

我使用此页面作为参考。 https://docs.fedoraproject.org/en-US/packaging-guidelines/Scriptlets/#_systemd https://docs.fedoraproject.org/en-US/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
Run Code Online (Sandbox Code Playgroud)

msu*_*chy 5

1)%{pkgname}.service应该放在%{_unitdir}其中扩展为/usr/lib/systemd/system/

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
Run Code Online (Sandbox Code Playgroud)

%pre和 也同样如此%preun