run script as another user from a root script with no tty stdin
You will need to disable the requiretty
setting in /etc/sudoers
for root
. Add the following line via visudo
:
Defaults:root !requiretty
You will also need the following line in /etc/sudoers
so root
can do everything (this should be enabled by default, but check to be sure):
root ALL=(ALL) ALL
Then you can do the following:
sudo -u training /path/to/training_command
This seems to be essentially a special case of this question; we can use script -c
to fake a tty. The only problem is that I can't get that trick to work directly with su
for some reason, and sudo
is a bit ugly if you really have to source ~/.bashrc
before training_command
. Try:
echo password | script -c "sudo su - training -c training_command"