Question

I need to run a command as a different user in the %post section of an RPM.

At the moment I am using a bit of a hack via python but it can't be the best way (it does feel a little dirty) ...

%post -p /usr/bin/python
import os, pwd, subprocess
os.setuid(pwd.getpwnam('apache')[2])
subprocess.call(['/usr/bin/something', 'an arg'])

Is there a proper way to do this?

Was it helpful?

Solution

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.

OTHER TIPS

The accepted answer here is wrong IMO. It is not often at all you want to set attributes to allow anyone execute something as the owner.

If you want to run something as a specific user, and that user doesn't have a shell set, you can use su -s to set the shell to use.

For example: su -s /bin/bash apache -c "/usr/bin/something an arg"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top