When scriptlets are called, they will be supplied with an argument. This argument, accessed via $1 (for shell scripts) is the number of packages of this name which will be left on the system when the action completes
All scriptlets MUST exit with the zero exit status.
Ordering
The scriptlets in %pre and %post are respectively run before and after a package is installed. The scriptlets %preun and %postun are run before and after a package is uninstalled. The scriptlets %pretrans and %posttrans are run at start and end of a transaction. On upgrade, the scripts are run in the following order:
- %pre of new package
(package install) - %post of new package
- %preun of old package
(removal of old package) - %postun of old package
I have created two rpms with two different version having the below content in the spec file
echo "-------------"
echo "This is pre"
echo "Install Value: $1"
echo "Upgrade Value: $1"
echo "Uninstall Value: $1"
echo "-------------"
%post
echo "-------------"
echo "This is post"
echo "Install Value: $1"
echo "Upgrade Value: $1"
echo "Uninstall Value: $1"
echo "-------------"
%preun
echo "-------------"
echo "This is preun"
echo "Install Value: $1"
echo "Upgrade Value: $1"
echo "Uninstall Value: $1"
echo "-------------"
%postun
echo "-------------"
echo "This is postun"
echo "Install Value: $1"
echo "Upgrade Value: $1"
echo "Uninstall Value: $1"
echo "-------------"
Installing the rpm
Below are the scriplets values when an rpm is installed
Preparing... ################################# [100%]
-------------
This is pre
Install Value: 1
Upgrade Value: 1
Uninstall Value: 1
-------------
Updating / installing...
1:deepak-1.0.0-1 ################################# [100%]
-------------
This is post
Install Value: 1
Upgrade Value: 1
Uninstall Value: 1
-------------
Updating the rpm
Preparing... ################################# [100%]
-------------
This is pre
Install Value: 2
Upgrade Value: 2
Uninstall Value: 2
-------------
Updating / installing...
1:deepak-2.0.0-1 ################################# [ 50%]
-------------
This is post
Install Value: 2
Upgrade Value: 2
Uninstall Value: 2
-------------
-------------
This is preun
Install Value: 1
Upgrade Value: 1
Uninstall Value: 1
-------------
Cleaning up / removing...
2:deepak-1.0.0-1 ################################# [100%]
-------------
This is postun
Install Value: 1
Upgrade Value: 1
Uninstall Value: 1
-------------
Removing an rpm
-------------
This is preun
Install Value: 0
Upgrade Value: 0
Uninstall Value: 0
-------------
-------------
This is postun
Install Value: 0
Upgrade Value: 0
Uninstall Value: 0
-------------
As as you see now we have the scriplets values which we must use. Below is the consolidated output
So lets use them to put in some scripting
Final Spec File
Time for the final spec file. here replace the respective space with the script content which you want to get executed
I have highlighted the print statements based on the action which will be performed. You can replace those print statements and place your scripts accordingly.
if [ $1 == 1 ];then
echo "-----------------------"
echo "RPM is getting installed"
echo "Put your script here"
echo "-----------------------"
elif [ $1 == 2 ];then
echo "-----------------------"
echo "RPM is getting upgraded"
echo "Put your script here"
echo "-----------------------"
fi
%post
if [ $1 == 1 ];then
echo "-----------------------"
echo "RPM is getting installed"
echo "Put your script here"
echo "-----------------------"
elif [ $1 == 2 ];then
echo "-----------------------"
echo "RPM is getting upgraded"
echo "Put your script here"
echo "-----------------------"
fi
%preun
if [ $1 == 1 ];then
echo "-----------------------"
echo "RPM is getting upgraded"
echo "Put your script here which will be called when this rpm is removed"
echo "-----------------------"
elif [ $1 == 0 ];then
echo "--------------------"
echo "RPM is getting removed/uninstalled"
echo "Put your script here which will be called before uninstallation of this rpm"
echo "--------------------"
fi
%postun
if [ $1 == 1 ];then
echo "-----------------------"
echo "RPM is getting upgraded"
echo "Put your script here which will be called when this rpm is removed"
echo "-----------------------"
elif [ $1 == 0 ];then
echo "--------------------"
echo "RPM is getting removed/uninstalled"
echo "Put your script here which will be called after uninstallation of this rpm"
echo "--------------------"
fi
Installing an rpm
Preparing... ################################# [100%]
-----------------------
RPM is getting installed
Put your script here
-----------------------
Updating / installing...
1:deepak-1.0.0-1 ################################# [100%]
-----------------------
RPM is getting installed
Put your script here
-----------------------
Upgrading an rpm
Preparing... ################################# [100%]
-----------------------
RPM is getting upgraded
Put your script here
-----------------------
Updating / installing...
1:deepak-2.0.0-1 ################################# [ 50%]
-----------------------
RPM is getting upgraded
Put your script here
-----------------------
-----------------------
RPM is getting upgraded
Put your script here which will be called when this rpm is removed
-----------------------
Cleaning up / removing...
2:deepak-1.0.0-1 ################################# [100%]
-----------------------
RPM is getting upgraded
Put your script here which will be called when this rpm is removed
-----------------------
Removing an rpm
--------------------
RPM is getting removed/uninstalled
Put your script here which will be called before uninstallation of this rpm
--------------------
--------------------
RPM is getting removed/uninstalled
Put your script here which will be called after uninstallation of this rpm
--------------------
I hope the article was useful.