Created
May 8, 2017 16:20
-
-
Save wcooley/f5d1c250ee6096add30c1d99c00a72f9 to your computer and use it in GitHub Desktop.
Snippet: Ensure script is run with correct user by re-running with sudo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ensure this script is run with the correct user by checking | |
RUNAS_USER="svcuser" | |
# Can use "$USER" instead of "$(id -un)" if the environment | |
# variable can be trusted; saves the exec(2) of the `id` which | |
# could be important -- because exec's are a bit more expensive | |
# and `id` can incur the overhead of a slow nameservice lookup. | |
# These would be important in a script used to shutdown or otherwise | |
# recover a system that is in distress due to resource starvation, | |
# for example. | |
if [[ "$(id -un)" != "$RUNAS_USER" ]]; then | |
# sudo: Include "-H" to initialize HOME; | |
# use "-i" instead for full initial login setup | |
exec sudo -H -u "$RUNAS_USER" "$0" "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment