Atomiklan
Atomiklan

Reputation: 5434

Bash dropping out of sudo in a script

I need to execute an install script using sudo, but towards the end of the script, the script needs to drop out of sudo and continue as the regular user.

Example:

sudo ./install.sh

script runs and does what it needs to as root

su myscriptuser
service myscript start

Basically, the service myscript start needs to be run by the regular user, not by root.

Upvotes: 0

Views: 695

Answers (1)

SzG
SzG

Reputation: 12619

su myscriptuser starts another shell in the name of myscriptuser and waits until it exits. Then it proceeds to run service myscript start in the name of root again.

What you need instead of the last 2 commands is sudo:

sudo -u myscriptuser service myscript start

Upvotes: 1

Related Questions