#!/bin/bash # # Container VNC start script # Copyright (C) 2024 Concurrent Real Time, Inc. # Version 1.0 2024/02/22 # # This software may be used and distributed according to the terms of # the GNU General Public License (GPL), version 2, or at your option # any later version. # export DISPLAY=`fgrep $USER /etc/tigervnc/vncserver.users|cut -d= -f1` export VNC_SESSION_DNO=`echo "$DISPLAY"|cut -d: -f2` export VNC_SESSION_UID=`fgrep $USER /etc/passwd|cut -d: -f3` export VNC_SESSION_DIR=`fgrep $USER /etc/passwd|cut -d: -f6` export HISTCONTROL=ignoredups export HISTSIZE=1000 export XDG_VTNR=1 export XDG_SESSION_ID=1 export XDG_SEAT=seat0 export XDG_RUNTIME_DIR=/run/user/$SESSION_UID export LANG=en_US.UTF-8 LOGFILE="/dev/null" myname=$(basename $0) usage() { echo "Usage: $myname [-l][-c]" echo " -l: Output log to /tmp/.vncserver.?.log" echo " -c: Use GNOME-Classic instead of GNOME" } # # Check /etc/tigervnc/vncserver.users # if [ -z "$DISPLAY" -o -z "$VNC_SESSION_DNO" ] then echo "Error : User $USER does not exist in /etc/tigervnc/vncserver.users" /usr/bin/bash exit 1 fi # # Check /etc/passwd # if [ -z "$VNC_SESSION_UID" ] then echo "Error : User $USER does not exist in /etc/passwd" /usr/bin/bash exit 2 fi while getopts "lc" c do case "$c" in c) export GNOME_SHELL_SESSION_MODE=classic export XDG_CURRENT_DESKTOP=GNOME-Classic:GNOME ;; l) LOGFILE="/tmp/.vncserver.$VNC_SESSION_DNO.log" touch $LOGFILE ;; \?) usage >&2; exit 1 ;; esac done trap "vncserver -kill $DISPLAY" 0 1 2 3 7 13 15 # # Failsafe # vncserver -kill $DISPLAY 2>&1 > /dev/null if [ -f /tmp/.X$VNC_SESSION_DNO-lock ] then rm -f /tmp/.X$VNC_SESSION_DNO-lock fi if [ -f /tmp/.X11-unix/X$VNC_SESSION_DNO ] then rm -f /tmp/.X11-unix/X$VNC_SESSION_DNO fi echo "--- Vncserver($DISPLAY) session started. ---" | tee -a $LOGFILE date | tee -a $LOGFILE ifconfig -a | tee -a $LOGFILE echo "vncserver -list $DISPLAY" | tee -a $LOGFILE vncserver -list $DISPLAY 2>&1 | tee -a $LOGFILE while true do vncserver $DISPLAY -xstartup /etc/X11/xinit/xinitrc -autokill -name $USER -rfbauth $VNC_SESSION_DIR/.vnc/passwd 2>&1 | tee -a $LOGFILE vncserver_pid=`vncserver -list 2>&1|tail -1|grep $DISPLAY|awk '{print$2}'` while [ -d "/proc/$vncserver_pid" ]; do if [ -f "/proc/$vncserver_pid/status" ] then State=`fgrep State /proc/$vncserver_pid/status` case "$State" in *\(zombie\)) echo "$USER($DISPLAY) logged out."| tee -a $LOGFILE break; ;; *) sleep 1; ;; esac else echo "Vncserver($DISPLAY) session has been terminated." | tee -a $LOGFILE break; fi done sleep 3 echo "--- Vncserver($DISPLAY) re-session started. ---" | tee -a $LOGFILE date | tee -a $LOGFILE done