I have a simple init.d script which starts and stops a process. I call the init.d as root, but I'd like the process it controls to run as a specific user.
The most common suggestion online seems to be to do
su myuser -c "my_process args"
However, this creates a second process space, new shell etc. and is somewhat inelegant.
I'd prefer to use
exec(), since it replaces the shell, but this doesn't take a user as argument. Is this a case where I should use
setuid() first? What about setting the gid? Are there any gotchas to be aware of?
Alternatively, are there distro-specific solutions to running the init.d as a different user? My environment is Centos 6.4.