diff --git a/scripts/_common.sh b/scripts/_common.sh index 63b4dd2..74869b1 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -173,3 +173,26 @@ EOF chmod +x "/etc/cron.daily/node_update" } + +# EXEC_LOGIN_AS Helper + +# Execute a command as another user with login +# (hence in user home dir, with prior loading of .profile, etc.) +# usage: exec_login_as USER COMMAND [ARG ...] +exec_login_as() { + local user=$1 + shift 1 + exec_as $user --login "$@" +} +# Execute a command as another user +# usage: exec_as USER COMMAND [ARG ...] +exec_as() { + local user=$1 + shift 1 + + if [[ $user = $(whoami) ]]; then + eval "$@" + else + sudo -u "$user" "$@" + fi +}