Skip to content

Instantly share code, notes, and snippets.

@taikedz
Last active July 10, 2018 10:59
Show Gist options
  • Save taikedz/112198ec7ec2fef8d2ded2799f94cf4b to your computer and use it in GitHub Desktop.
Save taikedz/112198ec7ec2fef8d2ded2799f94cf4b to your computer and use it in GitHub Desktop.
Run an X application as a different user, with some sound working
#!/usr/bin/env bash
die() {
echo "$*"
exit 1
}
turnoff_xhost() {
xhost - >/dev/null 2>&1
}
audio_user() {
local user="$1"; shift
if ! (groups "$user" | grep -e "\baudio\b" -q); then
echo "Adding $user to audio group."
echo "Password for $(whoami)"
sudo gpasswd -a "$user" audio || echo "Audio enablement problem - audio may not work"
fi
}
checkvar() {
local var="$1"; shift
[[ -n "$var" ]] || die "$* not specified"
}
main() {
if [[ "$WAITING_MODE" = true ]]; then
user="$1"; shift || {
echo "Specify the user to run as !"
}
trap turnoff_xhost exit
sudo -u "$user" -i "$@" >/dev/null 2>&1
else
checkvar "$1" user
checkvar "$2" program
audio_user "$1"
xhost + >/dev/null 2>&1 || die "Could not adjust xhost"
WAITING_MODE=true "$0" "$@" &
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment