1

I am using OpenSUSE and I have created a script that launches at startup the x11vnc server. But when the user logs out x11vnc is killed. I want it to start again automatically. Here the script I wrote. It works perfectly at boot.

#!/bin/sh
#
# /etc/init.d/vnc
#
### BEGIN INIT INFO
# Provides:          x11vnc server
# Required-Start:    xdm
# Should-Start: 
# Required-Stop: 
# Should-Stop: 
# Default-Start:     5
# Default-Stop:      0 1 2 6
# Short-Description: 
# Description:       Start or stop vnc server
### END INIT INFO


#INIT SCRIPT VARIABLES
SERVICE=$(basename $0)
#Gets the name of the script

BIN="/usr/bin/x11vnc"
#Binary path
ALLOWED_GROUP=$(getent group g_vnc-usr | awk -F ":" '{ print $4 }')
#Only inf-usr group is allowed to take control of any machine.

AUTH=`ps wwaux | grep '/X.*-auth' | sed -e 's/^.*-auth *//' -e 's/ .*$//' | head -n 1`

OPT="-display :0 -auth ${AUTH} -nopw -unixpw ${ALLOWED_GROUP} -shared -oa /var/log/vnc.log -xkb -bg -verbose -forever"
#Various options of the x11vnc providing auth, user auth, logging and "keep alive" connection.

CMD="${BIN} ${OPT}"
#Both bin and options are stored

. /lib/lsb/init-functions

rc_reset
# Reset status of this service

case "$1" in
    start)
    echo -n "Starting ${SERVICE}..."
        ## Start daemon with startproc(8). 
    /sbin/startproc ${CMD}

    ##>> /dev/null 2>&1
    sleep 2s

    # Remember status and be verbose.
        rc_status -v
    ;;

    stop)
    echo -n "Shutting down ${SERVICE}..."
    ## Stop daemon with killproc(8) 
    /sbin/killproc ${BIN}

    # Remember status and be verbose
    rc_status -v
        ;;

    restart)
    ## Stop the service and regardless of whether it was
    ## running or not, start it again.
    $0 stop
    $0 start

    # Remember status and be quiet
    rc_status
    ;;

    status)
    echo -n "Checking for service ${SERVICE}..."
    ## Check status with checkproc(8), if process is running
    ## checkproc will return with exit status 0.
    /sbin/checkproc ${BIN}

    # Remember status and be verbose
    rc_status -v
    ;;
    *)
    echo -n 
    echo -n "Usage: ${SERVICE} {start|stop|restart|status}"
    exit 1
    ;;
esac
rc_exit

This script allows any user from the group to take the machine over even if no one is currently logged in.

I wanted to use xinitrc and to add exec /etc/init.d/vnc restart

Thank you.

1
  • 1
    I finally added -loop at the end of the x11vnc command and it works. Create an outer loop restarting the x11vnc process whenever it terminates
    – igor012
    Jul 19 '12 at 14:41
1

(Converting a comment to an answer)

The -loop argument can be used to restart x11vnc in a loop. From the man page:

Create an outer loop restarting the x11vnc process whenever it terminates. -bg and -inetd are ignored in this mode (however see -loopbg below).

Useful for continuing even if the X server terminates and restarts (at that moment the process will need permission to reconnect to the new X server of course).

1

On a RHEL box, using the systemctl, I made the following change. I added an "/bin/at now" command to start the service a minute later. In my case, I changed the stop command to be:

ExecStop=/bin/sh -c '/usr/bin/vncserver -kill -9 %i > /dev/null 2>&1; echo "systemctl start vncserver@:1.service" | /bin/at now + 1 minute || :'

I edited: /etc/systemd/system/vncserver@:1.service

After making the change, run this command to update the system: systemctl daemon-reload

1
  • Pretty dangerous to add a self-recursive configuration like that. Consider the consequences when you actually want to stop it. Far better to use the systemd system as it's designed by just adding the correct directives to the unit file: Restart=on-success RestartSec=10
    – Blaine
    Mar 13 at 18:30
0

If using systemd, a far better solution than the other suggestions here is to add a systemd unit override file, as designed-for and recommended by the system authors.

This does exactly what's desired with minimal customization, and minimal impact to future maintenance and upgrades.

If your base systemd service (without the :INTEGER suffix) is named vncserver@ like mine is, you would create directory /etc/systemd/system/vncserver@.service.d/ and put this file into it with name override.conf.

[Service]
Restart=on-success
RestartSec=10

then run systemctl daemon-reload. Adjust RestartSec according to how fast your system eradicates the previous session process.

Instead of manually making the directory and file you could alternatively run

systemctl edit vncserver@

and enter the text above where the boilerplate text instructs. With this method you just need to know the base service name, not the directory, and the edit command will take care of the daemon-reload for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.