RPM SPEC Systemd 启用并启动

RPM SPEC Systemd enable and start

我已经创建了一个 RPM SPEC 文件,但我正在努力启用和启动 Systemd。通过 yum 更新软件包会禁用并停止该服务。 发行版为Centos7.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 [  -eq 2 ]; then
        /usr/bin/systemctl stop %{pkgname}.service >/dev/null 2>&1 ||:
      fi

    %post
    %systemd_post %{pkgname}.service

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

   %preun
   %systemd_preun %{pkgname}.service
    #old package
    #uninstall
    if [  -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 [  -eq 1 ]; then
      /usr/bin/systemctl --no-reload disable %{pkgname}.service
      /usr/bin/systemctl stop %{pkgname}.service
    fi

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

2) 当你使用 %systemd_post %{pkgname}.service 宏时,不需要有:

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

%pre%preun 类似。