1

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

1 Answer 1

4

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.

2
  • Is /usr/lib/systemd/system the folder to be used for user services? Do I set the %{_unitdir} in the SPEC file and should it contain the path plus the name of the service? Mar 28, 2019 at 18:06
  • 1
    The folder should be used for all services. The macro should be set by your system. Try rpm --eval '%{_unitdir}'.
    – msuchy
    Mar 29, 2019 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.