How to monitor TigerVNC service using Monit

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/
# start/stop vnc service as regular user

# vnc server binary

# vnc display

# run vnc as user

# vnc pid file

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

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

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

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;
  vnc_getpid > $vnc_pid # update pid file for monit
elif [ $# -eq 1 -a "$1" = "stop" -a -n "$(vnc_getpid)" ]; then
  unlink $vnc_pid
The interesting part is the way shell script manages PID of the running process using TigerVNC internal commands.

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/
  start program = "/usr/sbin/ start"
  stop  program = "/usr/sbin/ stop"

  depend on tigervnc_bin

  check file tigervnc_bin with path /usr/sbin/
    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