I know that title was a little ambiguous. Allow me to explain our issue. We designed an installer script on Ubuntu since Debian based systems are our primary core, however, some of our cloud based systems and some of our customers use non Debian based systems. As a result, our script is failing in a few critical places since we used sudo. I know the answer to our question is very basic, I just don't know the exact syntax I should be using to be sure we support a fairly large number of common distributions (CentOS, RHEL, Fedora, Ubuntu, Debian, etc)
Ok so here is the issue. Our script is an installer for our software. The script installs the required files, services, etc. The install script must run as root, so the user must either be at a root console, or use sudo. Either way, everything is good up till this point. At the end of the script, the installer MUST drop out of the root console and start the service as the software's user. To do this, when I first wrote the script, I used the following:
usermod -aG sudo <username> #Put the softwares user in the sudo group sudo -u <username> service <software> start #Drop to the user and start the service
Of course as soon as I tried to deploy this on our non Debian based systems, the script errored out since CentOS didn't understand sudo. The solution is NOT to enable sudo as this may also be true on some of our customers systems in which we don't have the ability to turn this on and off. So naturally I started looking into a way to do it with the older su command. I tried the following but it either errors out or drops out of the script immediately after changing users:
su <username> service <software> start
Any suggestions on the correct syntax so I can make this work on most Linux distributions?
* UPDATE *
I tried the following:
su <username> -c 'service <software> start'
The command would run, but fail because the user does not have root privileges. It specifically says it cant find the service command (again no root). So why does the above sudo command work? Does it run as the user, but with root privileges?