Originally posted by davholla
I want to write a shell script that switches user runs another script and then exits the user account.
The only problem I am having is that the script askes for the password but I want the password to be in the script (the script will be execute only so the user will not be able to see the password).
Quickes, easiest, least messy, and least security compromising way to do this is with SUDO. it should already be installed on your system, if not it's pretty easy to add. Use the command 'visudo' to edit the configuration.
You'll need to add a command alias:
And you'll need to add a line for the user to run this script
userName ALL=(userToRunAs) NOPASSWD: SOMECOMMAND
Then in your script to execute the command as the other user just write the command as:
sudo -u userToRunAs /path/to/the/script