blob: 1a3e0a27562698f0a72efb48b55169468c5cd7dc [file] [log] [blame]
San Mehata430b2b2014-09-23 08:30:51 -07001#!/bin/sh
2#
3# x11vnc_loop:
4#
5# Example startup script for connecting x11vnc to an X display
6# at system boot up and having it reconnect when the X server restarts.
7#
8# Run, in rc.local say, via, e.g.:
9#
10# /path/to/x11vnc_loop 1>> /var/tmp/x11vnc_loop.log 2>&1 &
11#
12# call with argument "once" or a number to limit the number of loops.
13#
14##########################################################################
15# The following needs to be customized:
16x11vnc_cmd=x11vnc # or use full path (or set PATH).
17pwfile=/path/to/vnc/passwd # always use a password
18display=:0 # display of interest
19restart_sleep=5 # pause between X server restarts.
20
21# modify cmdline args if desired:
22x11vnc_args="-display $display -rfbauth $pwfile -forever -nap"
23
24# you may need to customize the "grep", etc, below in get_xauthority_file()
25##########################################################################
26
27if [ "X$1" != "X" ]; then
28 max=$1
29 shift
30fi
31
32get_xauthority_file() {
33 #
34 # We need to find the MIT-COOKIE file... this not portable at all,
35 # depends on OS, distro, desktop, phase of moon, etc...
36 #
37 # If the cookie file was fixed and you knew it, you could just
38 # return it here e.g.:
39 #
40 ## echo "/var/gdm/:0.Xauth"; return
41 #
42 # or, if you knew the directory, you could look for the youngest
43 # file there and return it e.g.:
44 #
45 ## echo `ls -t /var/lib/xdm/authdir/authfiles/* | head -1`; return
46
47 # this hack tries to grep it out of ps output...
48 xauth=""
49 for i in 1 2 3
50 do
51 # very linux specific, and you likely need to tweak..
52 patt="X11R6.*/X.*-auth"
53 xauth=`ps wwwaux | grep "$patt" \
54 | egrep -v 'grep|Xprt' | head -1 \
55 | sed -e 's/^.*-auth//' | awk '{print $1}'`
56
57 if [ "X$xauth" != "X" ]; then
58 break
59 fi
60 sleep 2 # wait a bit in case X server is restarting slowly.
61 done
62 echo $xauth
63}
64
65try=1
66while [ 1 ]
67do
68 echo "`date` $0 try number: $try"; try=`expr $try + 1`
69
70 auth=`get_xauthority_file`
71 if [ ! -r "$auth" ]; then
72 echo "`date` bad auth file: \"$auth\""
73 else
74 cmd="$x11vnc_cmd $x11vnc_args"
75 sleep 1
76 echo "`date` running: $cmd -auth $auth"
77 # run x11vnc:
78 $cmd -auth $auth
79 if [ "X$max" = "Xonce" ]; then
80 exit $?
81 fi
82 fi
83 if echo "$max" | grep '[0-9]' > /dev/null; then
84 if [ $try -gt $max ]; then
85 exit
86 fi
87 fi
88 sleep $restart_sleep
89done