Skip to content
Advertisement

RPM SPEC Systemd enable and start

I have created a RPM SPEC file but I am struggeling with Systemd enabling and starting. Updating the package through yum disables and stops the service. The distribution is Centos 7.x

I have installed the service under /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

    #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

Advertisement

Answer

1) the %{pkgname}.service should be placed in %{_unitdir} which expands to /usr/lib/systemd/system/

2) When you use %systemd_post %{pkgname}.service macro there is no need to have there:

  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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement