mstormo

.xinit + script to run x0vncservers for all screens

Feb 8th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.36 KB | None | 0 0
  1. [me@mbeast ~]$ cat ~/.xinitrc
  2. xhost +localhost
  3. /home/me/.x0vnc-run &
  4.  
  5. [me@mbeast ~]$ cat ~/.x0vnc-run
  6. #!/bin/sh
  7.  
  8. while true
  9. do
  10.     # Get active screens
  11.     XSCREENS=$(xrandr | grep " connected" | perl -ne 'while (s/^\S+ connected[\s\D]* (\S+) .*$/$1/g) { print "$_"}')
  12.  
  13.     # Process input by newlines
  14.     IFS=$'\n'
  15.  
  16.     # Start with VNC port 5900
  17.     vncport=5900
  18.  
  19.     # Loop over each screen and start a separate VNC server    
  20.     LASTPID=0
  21.     for geometry in $XSCREENS
  22.     do
  23.         # Get a notification, so -n later makes sense
  24.         jobs &>/dev/null
  25.  
  26.         x0vncserver -PasswordFile /home/me/.vnc/passwd -display=:0 -rfbport=$vncport -Geometry=$geometry &
  27.  
  28.         # Get job changes
  29.         new_job_started="$(jobs -n)"
  30.  
  31.         # If we got something, we know the previous command is infact running, so grab its PID
  32.         if [ -n "$new_job_started" ];then
  33.             LASTPID=$!
  34.         else
  35.             LASTPID=
  36.         fi
  37.  
  38.         # Increase port count, for next VNC instance
  39.         vncport=$((vncport+1))
  40.     done
  41.  
  42.     # After creating all VNC sessions, now wait for the last one to exit (likely due to resolution changes)
  43.     while s=`ps -p $LASTPID -o s=` && [[ "$s" && "$s" != 'Z' ]]; do
  44.         sleep 1
  45.     done
  46.  
  47.     # Pause slightly before respawning the VNC server, giving the Xorg a little time to set up
  48.     sleep 5
  49. done
Advertisement
Add Comment
Please, Sign In to add comment