2023-05-01 00:11:01 +02:00
|
|
|
#!/bin/sh
|
2023-05-14 16:02:54 +02:00
|
|
|
# shellcheck disable=SC2317
|
2023-05-01 00:11:01 +02:00
|
|
|
|
|
|
|
# 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."
|
2023-05-12 15:18:32 +02:00
|
|
|
verbs="start|status|stop|restart|help"
|
2023-05-01 00:11:01 +02:00
|
|
|
|
2023-05-07 22:08:11 +02:00
|
|
|
echol() {
|
|
|
|
col="$1"
|
|
|
|
shift
|
|
|
|
printf '\033['"${col}"'m%s\033[0m\n' "$*"
|
|
|
|
}
|
|
|
|
|
2023-05-01 00:11:01 +02:00
|
|
|
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:
|
2023-05-12 15:18:32 +02:00
|
|
|
${name} ${verbs}
|
2023-05-01 00:11:01 +02:00
|
|
|
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
|
2023-05-20 10:42:13 +02:00
|
|
|
vncserver \
|
2023-05-20 23:25:21 +02:00
|
|
|
-xstartup xstartup \
|
2023-05-20 10:42:13 +02:00
|
|
|
-localhost \
|
|
|
|
-alwaysshared \
|
|
|
|
-securitytypes none
|
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
|
2023-05-07 22:08:11 +02:00
|
|
|
echol '1;32' "The VNC server is running."
|
|
|
|
tail "$log_f"
|
2023-05-01 00:11:01 +02:00
|
|
|
else
|
2023-05-07 22:08:11 +02:00
|
|
|
echol '1;31' "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
|
|
|
|
}
|
|
|
|
|
2023-05-12 14:59:13 +02:00
|
|
|
restart() {
|
|
|
|
stop
|
|
|
|
start
|
|
|
|
}
|
|
|
|
|
2023-05-01 00:11:01 +02:00
|
|
|
# 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
|
2023-05-12 15:26:03 +02:00
|
|
|
status|start|stop|restart|help)
|
2023-05-01 00:11:01 +02:00
|
|
|
"$arg"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
usage "Invalid argument \"$arg\""
|
2023-04-13 00:04:20 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|