How to run and interact with a script from within an RPM?
You are mixing several concepts here. So let make step back.
In %prep
section you should unpack your %{SOURCE0}
and apply patches if any. This usually do
%setup -q
However if you want you can extract it manually. For more info about this macro see http://www.rpm.org/max-rpm/s1-rpm-inside-macros.html
In %build
section you usually compile source into binaries. Likely empty if you use interpreted language or your tar contains already compiled binaries.
In %install
section you should copy the files into %{buildroot}
and create there structure which will land in package. E.g. %{buildroot}/etc/yourconfig
, %{buildroot}/usr/bin/yourcommand
etc. You can run there any script you want, but keep in mind that it is run only in build time. I.e. only on your machine (or build system). This is intended for creating files which are automatically generated (e.g. documentation of libraries from source code).
Then you have section %post
which is run on user machine after the package was installed. And all files are installed in final path. Not in buildroot. At the beginning you are changed to /
so you need to specify full path on that user machine.
So in your case it should be probably look like:
%install
mkdir -p %{buildroot}%{_bindir}
cp -a %{installscript} %{buildroot}%{_bindir}/
chmod a+x %{buildroot}%{_bindir}/%{installscript}
%files
%{_bindir}/%{installscript}
%post
%{_bindir}%{installscript}
Sever notes:
%post
section is executed under root, so sudo is not needed.Running interactive script is strongly discouraged. RPM was designed as non-interactive and every utility around assume no interaction during package installation (e.g. PackageKit, Spacewalk etc.). So sooner then later you will get some compains. It is much safer to say user to run some command after installation manually (or automate it using Ansible or Puppet).
Passing user defined argument to RPM is possible while installing?
RPMs aren't meant to take user defined arguments.
See RPM - Install time parameters
Another similar question is at https://superuser.com/questions/408852/is-it-possible-to-get-users-input-during-installation-of-rpm
One workaround is to have the rpm's postinstall script ask for input from stdin, in which case you can pass in the answers by redirecting stdio from a file or here document.
>rpm -i sample.rpm <<__NOT_RECOMMENDED__
somearg
__NOT_RECOMMENDED__
RPM %post as a different user
If /usr/bin/something
is something you are installing as part of the package, install it with something like
attr(4755, apache, apache) /usr/bin/something
When installed like this, /usr/bin/something
will always run as user apache
, regardless of what user actually runs it.
How would I make read command work in RPM spec file?
Rather than embed the read
within your package, RPM has a conditional mechanism which can be used via command-line parameters. Most usage of conditionals in RPMs tests constants defined in the system's RPM macros or making simple filesystem checks. You should investigate those first, because it allows your package to install without help from the person doing the install.
Here are some useful pages discussing RPM conditionals:
- Passing conditional parameters into a rpm build (rpm.org)
- PackagerDocs/ConditionalBuilds (rpm.org)
- Conditionals (Maximum RPM: Taking the Red Hat Package Manager to the Limit)
- openSUSE:RPM conditional builds
As one can see from the suggested reading, these are build-time rather than install-time features. You cannot make an "interactive" RPM install. To read more about that, see these pages:
- Is it possible to get user's input during installation of rpm?
- RPM - Install time parameters
The latter is clear that this is intentional on the part of the developers. As an aside, one response mentions the --relocate
option, implying that this solves the problem. However, it is actually different. Read more about that here:
- Relocatable packages
- Chapter 15. Making a Relocatable Package (Maximum RPM)
Related Topics
Segmentation Fault with a Variable in Section .Data
Assembly Call Subprograms Based on User Input
Idle Time of a Process in Linux
How to Make a Programme Executable Anywhere in the Shell
How to Make Git-Bash Command Line Start Up with Home Directory
Find Files with a Certain Extension That Exceeds a Certain File Size
No Console Output Available on Linux When Executing Grails/Groovy
Write Failing Without Error on Qspi (Spansion S25Hs512T) - U-Boot
How to Determinate Destination MAC Address
Replace Bash Variables in Template File
Usb Modem Is Echoing Back Wrong Characters
Rsync, 'Uid/Gid Impossible to Set' Cases Cause Future Hard Link Failure, How to Fix