Recently, I was searching for an easy to manage VNC service to use it at home. After a short while of consideration, I decided to install TigerVNC and manage it using Monit.

Shell script

This is a shell script used to start/stop VNC service.

Please notice that service is started as regular milosz user using display 31.

I assume that TigerVNC was extracted into /opt directory and VNC password was generated for the mentioned user.

# cat /usr/sbin/vncservice.sh
#!/bin/sh
# start/stop vnc service as regular user

# vnc server binary
vnc_bin="/opt/usr/bin/vncserver"

# vnc display
vnc_dsp="31"

# run vnc as user
vnc_usr="milosz"

# vnc pid file
vnc_pid="/var/run/tigervnc_${vnc_usr}_${vnc_dsp}.pid"

# get PID using internal vnc commands
vnc_getpid()
{
su $vnc_usr << EOF
$vnc_bin -list | sed -n -e "1,4d;s/:${vnc_dsp}[\t]\+\([0-9]\+\)/\1/p";
EOF
}

# start vnc service
vnc_start()
{
su $vnc_usr << EOF
$vnc_bin :${vnc_dsp} > /dev/null
EOF
}

# stop vnc service
vnc_stop()
{
su $vnc_usr << EOF
$vnc_bin -kill :${vnc_dsp} > /dev/null
EOF
}

if [ $# -eq 1 -a  "$1" = "start" ]; then
  if [ -n "$(vnc_getpid)" ]; then
    kill -0 $(vnc_getpid) 2>/dev/null
    if [ $? -eq 0 ]; then
      exit 2;
    else
      vnc_stop
    fi
  fi
  vnc_start
  vnc_getpid > $vnc_pid # update pid file for monit
elif [ $# -eq 1 -a "$1" = "stop" -a -n "$(vnc_getpid)" ]; then
  vnc_stop
  unlink $vnc_pid
fi

Monit configuration

Use defined PID file [which name consists of the combined username and display number] and the code listed in the previous section to manage service using Monit.

# cat /etc/monit/conf.d/tigervnc.conf
check process tigervnc with pidfile /var/run/tigervnc_milosz_31.pid
  start program = "/usr/sbin/vncservice.sh start"
  stop  program = "/usr/sbin/vncservice.sh stop"

  depend on tigervnc_bin

  check file tigervnc_bin with path /usr/sbin/vncservice.sh
    if failed checksum       then unmonitor
    if failed permission 755 then unmonitor
    if failed uid root       then unmonitor
    if failed gid root       then unmonitor

Sample summary information.

$ sudo monit summary
The Monit daemon 5.4 uptime: 16h 25m

Process 'tigervnc'                   Running
File 'tigervnc_bin'                  Accessible

Sample status information.

$ sudo monit status
The Monit daemon 5.4 uptime: 16h 25m

Process 'tigervnc'
  status                            Running
  monitoring status                 Monitored
  pid                               30886
  parent pid                        1
  uptime                            16h 22m
  children                          0
  memory kilobytes                  16188
  memory kilobytes total            16188
  memory percent                    0.7%
  memory percent total              0.7%
  cpu percent                       0.0%
  cpu percent total                 0.0%
  data collected                    Tue, 30 Sep 2014 18:38:40

File 'tigervnc_bin'
  status                            Accessible
  monitoring status                 Monitored
  permission                        755
  uid                               0
  gid                               0
  timestamp                         Tue, 30 Sep 2014 18:38:40
  size                              521576 B
  checksum                          13eb78d2ee07aba229f803140325fe7b (MD5)
  data collected                    Tue, 30 Sep 2014 18:38:40
ko-fi