Skip to content

Instantly share code, notes, and snippets.

@ensonic
Last active September 14, 2015 09:44
Show Gist options
  • Save ensonic/13a14012a152d976a27b to your computer and use it in GitHub Desktop.
Save ensonic/13a14012a152d976a27b to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# bricklaunch.sh <program>
# - open a new vt and switch to it
# - give 'user' ownership of the vt
# - run the given programm on the new vt as 'user'
#
# TODO:
# - tell caller which tty was activated
#
OPTIND=1 # Reset in case getopts has been used previously in the shell.
ttyfile=$(mktemp)
user='user'
openvt_opts=""
while getopts "swu:" opt; do
case "$opt" in
s) openvt_opts="$openvt_opts --switch"
;;
w) openvt_opts="$openvt_opts --wait"
;;
u) user=$OPTARG
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
/bin/openvt $openvt_opts -- \
/bin/bash -c "/usr/bin/tty >${ttyfile};/bin/chown ${user}: \$(/bin/cat ${ttyfile});/usr/bin/sudo --login --set-home --non-interactive --user=${user} -- \"$@\" || sleep 5s"
rm ${ttyfile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment