2023-05-01 00:11:01 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# cd to the local VNC directory, exit if it fails
|
2023-05-01 00:17:12 +02:00
|
|
|
cd "${HOME}/.vnc" || exit 5
|
2023-04-13 00:04:20 +02:00
|
|
|
|
|
|
|
# Variables
|
2023-05-01 00:11:01 +02:00
|
|
|
file_base="./$(hostname):1."
|
|
|
|
|
|
|
|
error() {
|
|
|
|
>&2 printf '\033[1;31m%s\033[0m %s\n' "ERROR:" "$1"
|
|
|
|
shift
|
|
|
|
exit "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
help() {
|
|
|
|
name="$(basename "$0")"
|
|
|
|
cat << EOF
|
|
|
|
${name} - Start a VNC server
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
${name} start|status|stop|help
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
>&2 help
|
|
|
|
error "Invalid usage: ${1}" 1
|
|
|
|
}
|
|
|
|
|
|
|
|
is_running() {
|
|
|
|
vncserver -list | grep -q '^:1'
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
if ! is_running; then
|
2023-05-07 21:59:23 +02:00
|
|
|
set -e
|
|
|
|
vncserver > /dev/null
|
|
|
|
echo 'export DISPLAY=:1;'
|
2023-05-01 00:11:01 +02:00
|
|
|
else
|
2023-05-01 00:17:12 +02:00
|
|
|
error "The VNC server is already running!" 4
|
2023-05-01 00:11:01 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
status() {
|
|
|
|
log_f="${file_base}log"
|
|
|
|
if is_running; then
|
|
|
|
tail -f "$log_f"
|
|
|
|
else
|
2023-05-01 00:17:12 +02:00
|
|
|
echo "The VNC server is not running."
|
2023-05-01 00:11:01 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
if is_running; then
|
|
|
|
vncserver -kill :1
|
|
|
|
else
|
|
|
|
error "The VNC server is not running!" 3
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Argument parsing
|
|
|
|
set -e; trap 'set +e; error "$error" "$?"' EXIT
|
|
|
|
error="You must give an argument"
|
|
|
|
[ -n "$1" ]; arg="$1"; shift
|
|
|
|
unset error
|
|
|
|
set +e; trap - EXIT
|
|
|
|
|
|
|
|
|
|
|
|
# Main case statement
|
|
|
|
case "$arg" in
|
|
|
|
start|status|stop|help)
|
|
|
|
"$arg"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
usage "Invalid argument \"$arg\""
|
2023-04-13 00:04:20 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|