Allocate pseudo TTY in Upstart configuration file

Fred Clausen asked:

We would like to use Upstart to manage a Java process and run it as non-root using sudo. We are on RHEL 6 which means that Upstart is too old to allow us to explicitly specify a user hence the use of sudo.

However, the Sudo configuration file specifies Defaults requiretty which means we can’t run sudo -u to launch the command as a non-root user. We get the following error

sudo: sorry, you must have a tty to run sudo

Is there a way to get Upstart to allocate a pseudo TTY so that sudo will allow us to launch this process? The sudoers file is set according company policy and we are unable to change it.

My answer:

You don’t need to – and shouldn’t – use sudo to change users in an init script. Use su or better, runuser instead. The runuser command should be available from RHEL 5 and will do the right thing with respect to SELinux contexts, etc.

runuser -u username -g groupname command arguments

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.