Skip to content

Instantly share code, notes, and snippets.

@DaveThw
Last active November 30, 2018 07:37
Show Gist options
  • Save DaveThw/963f2b6c2670d159a88b to your computer and use it in GitHub Desktop.
Save DaveThw/963f2b6c2670d159a88b to your computer and use it in GitHub Desktop.
init.d script for vncserver (on a Raspberry Pi)
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: vncserver
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start VNC Server
### END INIT INFO
# Can be downloaded and installed in one go by using this command
# sudo wget -O /tmp/download https://gist.github.com/DaveThw/963f2b6c2670d159a88b/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/vncserver && sudo update-rc.d vncserver defaults
# Or download and just move to /etc/init.d using this command
# sudo wget -O /tmp/download https://gist.github.com/DaveThw/963f2b6c2670d159a88b/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/vncserver
# and then start/stop/restart the service with these commands
# sudo service vncserver stop
# sudo service vncserver start
# sudo service vncserver restart
# and check if the service is running with
# sudo service vncserver status
# And then, if/when you want VNC to autostart on bootup, use this
# sudo update-rc.d vncserver defaults
# To prevent autostart on bootup, use this
# sudo update-rc.d -f vncserver remove
# this script borrowed from http://www.everydaylinuxuser.com/2014/03/connect-to-raspberry-pi-from-hp.html
# then edited a bit by Dave
# then also updated a bit, to look more like a 'proper' init.d script - mostly guesswork so far...
# and then updated some more to make it give a 'nicer' output, and suchlike...
export USER="pi"
# parameters for tightvncserver
# Display number:
DISPLAY="1"
if [ "$2" != "" ]; then DISPLAY=$2; fi
# Bit depth:
DEPTH="16"
# Screen size:
# 1366x768 is full screen on my chromebook
# GEOMETRY="1366x768"
# 1348x768 is a little narrower, to work well when maximised (but not full screen)
GEOMETRY="1348x768"
# Name for the display:
NAME="VNCServer"
# DON'T CHANGE anything below this line unless you know what you're doing!
OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
. /lib/lsb/init-functions
HOST=$(hostname)
PID_FILE="/home/${USER}/.vnc/${HOST}:${DISPLAY}.pid"
DAEMON=/usr/bin/Xtightvnc
case "$1" in
start)
log_action_begin_msg "Starting vncserver on ${HOST}:${DISPLAY} as user '${USER}'"
vnc_status="0"
pidofproc -p "$PID_FILE" "$DAEMON" >/dev/null || vnc_status="$?"
status="0"
if [ "$vnc_status" = 0 ]; then
log_progress_msg "vncserver already running"
result=""
else
result=`su ${USER} -c "/usr/bin/vncserver ${OPTIONS}" 2>&1` || status=$?
fi
if [ ! -z "$result" ]; then
# We have one or more line(s) of additional information to show the user
# If would be nice to pad them out somehow, to match the indent caused by the left info
# block on the log_action_begin_msg line - if the info block is present...
# # First, pad the beginning of each line to match the indent caused by the left info block
# # Annoyingly, we can't use something like PAD=`log_begin_msg ""` - I think that the command
# # substitution causes log_begin_msg to be run in a new shell, and that shell doesn't meet
# # the requirements of log_use_fancy_output, so log_begin_msg doesn't print the left info
# # block that we want it to! (Plus, log_begin_msg seems to throw an error if it is passed
# # an empty argument, which seems to stop the script in its tracks)
# if log_use_fancy_output; then
# PAD="[....] "
# else
# PAD=""
# fi
# PAD=`printf %${#PAD}s`
# result=`echo "$result" | sed "s/^/$PAD/"`
# #result=`echo "$result" | sed 's/^/ /'`
# # move to a new line, as we are currently at the end of the initial line
# echo
# # display the additional line(s)
# log_progress_msg "$result"
# # finally, on a new line, display a final message to which we can add our status with log_end_msg
# echo
# log_begin_msg "...vncserver started"
# move to a new line, as we are currently at the end of the initial line
echo
# There's a slight fudge in here to 'hide' the '.' character that log_action_msg adds to
# the end of each line - I'm making it black, then restoring colour to normal ready for
# the next line...
# We could use log_begin_msg instead of log_action_msg... It produces a left info box with
# [....] instead of [info], and it doesn't like blank lines - but it doesn't add a '.' to
# the end of each line...
BLACK=$( $TPUT setaf 0)
NORMAL=$( $TPUT op)
# display the additional line(s) as 'info' lines...
echo "$result" | while IFS= read -r LINE; do
# Use this if statment to remove blank lines...
# if [ ! -z "$LINE" ]; then
# I'm currently liking having a little extra indent on these additional lines...
# (probably not very 'standard', but hey!)
# Maybe with a vertical line to tie the first and last lines together..?
log_action_msg " | $LINE$BLACK"; echo -n "$NORMAL"
#log_begin_msg " | ${LINE:- }"; echo
# fi
done
# display an end message to which we can add our final status with log_end_msg
if [ "$status" = 0 ]; then
log_begin_msg "...vncserver started"
else
log_begin_msg "...vncserver not started"
fi
fi
log_end_msg $status
;;
stop)
log_action_begin_msg "Stopping vncserver on ${HOST}:${DISPLAY} as user '${USER}'"
vnc_status="0"
pidofproc -p "$PID_FILE" "$DAEMON" >/dev/null || vnc_status="$?"
status="0"
if [ "$vnc_status" = 0 ]; then
result=`su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}" 2>&1` || status=$?
else
log_progress_msg "vncserver not currently running"
result=""
fi
if [ ! -z "$result" ]; then
BLACK=$( $TPUT setaf 0)
NORMAL=$( $TPUT op)
echo
echo "$result" | while IFS= read -r LINE; do
log_action_msg " | $LINE$BLACK"; echo -n "$NORMAL"
done
if [ "$status" = 0 ]; then
log_begin_msg "...vncserver stopped"
else
log_begin_msg "...vncserver (not?) stopped"
fi
fi
log_end_msg $status
;;
restart)
$0 stop $2
$0 start $2
;;
status)
status_of_proc -p ${PID_FILE} "$DAEMON" "vncserver on ${HOST}:${DISPLAY}"
exit $?
;;
*)
echo "Usage: $0 {start|stop|restart|status} [DISPLAY#]"
exit 1
esac
exit 0
@Pratiwir
Copy link

I used this to create a vncserver service on raspberry pi stretch. One problem was that in the vncserver file lines 105, 106, 139 and 140 the $TPUT call creates an error on bootup and the service thus fails. It seems to be an error of the form 'no value for $TERM and no -T specified' in the function call.
I commented out the function definitions and removed the $BLACK and $NORMAL references on lines 114 and 143.
The system then boots normally.
Maybe there is a better solution to this problem, I am not clear what the original problem was that necessitated the hack.

@Pratiwir
Copy link

On the Pi 3 with stretch there is another problem. Line 52 the name parameter isn't accepted in that version of the software.
If I remove -name ${NAME} then it starts the service normally.
I am just noting these issues for other users.
FYI, trying to get a remote desktop over tor working, for remote access. Anyone interested, same username on gmail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment