Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does service stop after RPM is updated

Tags:

redhat

rpm

I have a software package for which I created an RPM. I can't paste the entire RPM here for IP reasons, but here is the gist of the problem:

%pre
/sbin/pidof program
if [ "$?" -eq "0" ]
then
  /sbin/service program stop
fi

%post
/sbin/chkconfig program on
/sbin/service program start

%preun
/sbin/service program stop
/sbin/chkconfig program off

%postun
rm -rf /program_folder

Everytime I try to upgrade the package, it stops the program service, installs everything, starts the service, and then stops it again and deletes the folder...any ideas?

like image 596
Sagar Avatar asked Jan 13 '12 17:01

Sagar


People also ask

What is an rpm update?

RPM (Red Hat Package Manager) is an default open source and most popular package management utility for Red Hat based systems like (RHEL, CentOS and Fedora). The tool allows system administrators and users to install, update, uninstall, query, verify and manage system software packages in Unix/Linux operating systems.

How do I force an rpm to install?

The --force option will reinstall already installed packages or overwrite already installed files from other packages. You don't want this normally. If you tell rpm to install all RPMs from some directory, then it does exactly this. rpm will not ignore RPMs listed for installation.


1 Answers

This has to do with the order in which the scripts are ran :

%pre of new package
(package install)
%post of new package
%preun of old package
(removal of old package)
%postun of old package

So in your case, the %preun of the old rpm is ran last, which shuts off the service.

This can be solved by looking at the argument to the post script. Here is a table for the value:

          install    upgrade  uninstall
%pre      $1 == 1   $1 == 2   (N/A)
%post     $1 == 1   $1 == 2   (N/A)
%preun    (N/A)     $1 == 1   $1 == 0
%postun   (N/A)     $1 == 1   $1 == 0

So, you'd want your %postun script to instead be this:

%preun
if [ "$1" = "0" ]; then
    /sbin/service program stop
    /sbin/chkconfig program off
fi
exit 0

That will make it stop the program only on complete uninstall (ie; rpm -e) and should do it for you.

NOTE: you'll want to do the same with your %postun script as well

NOTE: the exit 0 at the end; I like to specifically put that there just in case, as a bad exit code from the last command will carry over and cause the script to exit with that status, causing problems with the rpm installation.

NOTE: fixing this in the new rpm doesn't fix it in the currently installed rpm. You'll want to rpm -e the old rpm with the broken script, and you will be fine moving forward.

This just covers the pre/post scripts; a more detailed version with triggers and such can be found here.

like image 64
Corey Henderson Avatar answered Sep 17 '22 20:09

Corey Henderson