LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Robust vncserver startup file (rc.vncserver) (http://www.linuxquestions.org/questions/slackware-14/robust-vncserver-startup-file-rc-vncserver-248779/)

arobinson74 10-29-2004 12:28 PM

Robust vncserver startup file (rc.vncserver)
 
Just wanting to share a script...

Here is a vnc server script (assumes vncserver is installed at /usr/local/bin/vncserver). Note: written for TightVNC 1.2.9 but should work for any vncserver):

Usage:
rc.vncserver start|stop [console_num]
-or-
rc.vncserver start_all|stop_all|status

First create a config file (/etc/rc.d/rc.vncserver.conf by default):
Code:

# configuration file for VNC
# format:
# number user options
1 someuser -name 1152 -geometry 1152x864

Then create the startup script (/etc/rc.d/rc.vncserver by default):
Code:

#!/bin/sh

CONF_FILE=/etc/rc.d/rc.vncserver.conf

if [ ! -f ${CONF_FILE} ] ; then
        echo "Unable to find configuration file"
        exit 1
fi

start_number()
{
        VNCNUM=$1
        LINE=`/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -P "^${VNCNUM}\b"`
        if [ "$LINE" = "" ] ; then
                echo "No definition found for terminal ${VNCNUM}"
                return 1
        fi

        RUN_AS_USER=`echo "${LINE}" | cut -d" " -f2`

        # make sure the user exists
        if ! /usr/bin/grep "${RUN_AS_USER}" /etc/passwd 1> /dev/null ; then
                echo "No such user: ${RUN_AS_USER}"
                return 2
        fi

        VNC_ARGS=`echo "${LINE}" | cut -d " " -f 3-15`

        #echo "/bin/su - ${RUN_AS_USER} -c ""/usr/local/bin/vncserver :${VNCNUM} ${VNC_ARGS}"""
        /bin/su - ${RUN_AS_USER} -c "/usr/local/bin/vncserver :${VNCNUM} ${VNC_ARGS}"
}

stop_number()
{
        VNCNUM=$1
        LINE=`/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -P "^${VNCNUM}\b"`
        if [ "$LINE" = "" ] ; then
                echo "No definition found for terminal ${VNCNUM}"
                return 1
        fi

        RUN_AS_USER=`echo "${LINE}" | cut -d" " -f2`

        # make sure the user exists
        if ! /usr/bin/grep "${RUN_AS_USER}" /etc/passwd 1> /dev/null ; then
                echo "No such user: ${RUN_AS_USER}"
                return 2
        fi

        VNC_ARGS=`echo "${LINE}" | cut -d " " -f 3-15`

        #echo "/bin/su - ${RUN_AS_USER} -c ""/usr/local/bin/vncserver -kill :${VNCNUM}"""
        if [ -f "/home/${RUN_AS_USER}/.vnc/linux:${VNCNUM}.pid" ] ; then
                /bin/su - ${RUN_AS_USER} -c "/usr/local/bin/vncserver -kill :${VNCNUM}"
        fi
}

status_number()
{
        VNCNUM=$1
        LINE=`/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -P "^${VNCNUM}\b"`
        if [ "$LINE" = "" ] ; then
                echo "No definition found for terminal ${VNCNUM}"
                return 1
        fi

        RUN_AS_USER=`echo "${LINE}" | cut -d" " -f2`

        # make sure the user exists
        if ! /usr/bin/grep "${RUN_AS_USER}" /etc/passwd 1> /dev/null ; then
                echo "No such user: ${RUN_AS_USER}"
                return 2
        fi

        VNC_ARGS=`echo "${LINE}" | cut -d " " -f 3-15`

        if [ -f "/home/${RUN_AS_USER}/.vnc/linux:${VNCNUM}.pid" ] ; then
                echo ":${VNCNUM} running as ${RUN_AS_USER}. PID: `cat /home/${RUN_AS_USER}/.vnc/linux:${VNCNUM}.pid`"
        else
                echo ":${VNCNUM} is not running"
        fi
}

start_all()
{
        for num in `/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -v -P "^\s*#" | cut -d" " -f1` ; do
                start_number $num
        done
}

stop_all()
{
        for num in `/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -v -P "^\s*#" | cut -d" " -f1` ; do
                stop_number $num
        done
}

status()
{
        for num in `/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -v -P "^\s*#" | cut -d" " -f1` ; do               
                status_number $num
        done
}

case "$1" in
'start')
        if [ "$2" = "" ] ; then
                echo "Console number missing"
                exit 2
        fi

        start_number $2
        ;;
       
'start_all')
        start_all
        ;;

'status')
        status
        ;;

'stop_all')
        stop_all
        ;;
       
'stop')
        if [ "$2" = "" ] ; then
                echo "Console number missing"
                exit 2
        fi
        stop_number $2
        ;;
       
*)
        echo "Usage:"
        echo "rc.vncserver start|stop [console_num]"
        echo " -or-"
        echo "rc.vncserver start_all|stop_all|status"
        exit 0
        ;;
esac

The *_all commands are not the best in performance since it searches the config file more than once, but it does not seem to hurt (I'm a better perl programmer then bash).

Enjoy

arobinson74 10-29-2004 12:32 PM

Just a note, for the vncserver to run, the path SU uses, must contain the X bin directory

Extend the ENV_PATH (This should be in "/etc/login.defs"):
ENV_PATH PATH=...(exiting path)...:/usr/X11R6/bin

arobinson74 10-29-2004 12:34 PM

Forgot to mention, change the
"/home/${RUN_AS_USER}/.vnc/linux:${VNCNUM}.pid"
lines in the code to the correct pid file (change the generic "linux" to your computer name)

arobinson74 10-29-2004 01:20 PM

Here is a new version that lets root or the "owner user" start and stop their own entries
(I also fixed the hostname file issue)

Code:

#!/bin/sh
 
# Configuration
CONF_FILE=/etc/rc.d/rc.vncserver.conf
HOST=`/bin/hostname`

# ensure the user env variable is correct
USER=`/usr/bin/id | /usr/bin/cut -d "(" -f2 | cut -d ")" -f1`

if [ ! -f ${CONF_FILE} ] ; then
        echo "Unable to find configuration file"
        exit 1
fi

start_number()
{
        VNCNUM=$1
        LINE=`/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -P "^${VNCNUM}\b"`
        if [ "$LINE" = "" ] ; then
                echo "No definition found for terminal ${VNCNUM}"
                return 1
        fi

        RUN_AS_USER=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d" " -f2`

        # make sure the user exists
        if ! /usr/bin/grep "${RUN_AS_USER}" /etc/passwd 1> /dev/null ; then
                echo "No such user: ${RUN_AS_USER}"
                return 2
        fi

        VNC_ARGS=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d " " -f 3-15`

        if [ "${RUN_AS_USER}" = "${USER}" ] ; then
                /usr/local/bin/vncserver :${VNCNUM} ${VNC_ARGS}
        elif [ "${USER}" = "root" ] ; then
                /bin/su - ${RUN_AS_USER} -c "/usr/local/bin/vncserver :${VNCNUM} ${VNC_ARGS}"
        else
                echo "You must be root or ${RUN_AS_USER} to start :${VNCNUM}"
        fi
}

stop_number()
{
        VNCNUM=$1
        LINE=`/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -P "^${VNCNUM}\b"`
        if [ "$LINE" = "" ] ; then
                echo "No definition found for terminal ${VNCNUM}"
                return 1
        fi

        RUN_AS_USER=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d" " -f2`

        # make sure the user exists
        if ! /usr/bin/grep "${RUN_AS_USER}" /etc/passwd 1> /dev/null ; then
                echo "No such user: ${RUN_AS_USER}"
                return 2
        fi

        VNC_ARGS=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d " " -f 3-15`

        if [ ! "${USER}" = "root" ] && [ ! "${RUN_AS_USER}" = "${USER}" ] ; then
                echo "You must be root or ${RUN_AS_USER} to stop VNC server :${VNCNUM}"
        else
                if [ -f "/home/${RUN_AS_USER}/.vnc/${HOST}:${VNCNUM}.pid" ] ; then
                        if [ "${RUN_AS_USER}" = "${USER}" ] ; then
                                /usr/local/bin/vncserver -kill :${VNCNUM}
                        else
                                /bin/su - ${RUN_AS_USER} -c "/usr/local/bin/vncserver -kill :${VNCNUM}"
                        fi
                fi
        fi
}

status_number()
{
        VNCNUM=$1
        LINE=`/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -P "^${VNCNUM}\b"`
        if [ "$LINE" = "" ] ; then
                echo "No definition found for terminal ${VNCNUM}"
                return 1
        fi

        RUN_AS_USER=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d" " -f2`

        # make sure the user exists
        if ! /usr/bin/grep "${RUN_AS_USER}" /etc/passwd 1> /dev/null ; then
                echo "No such user: ${RUN_AS_USER}"
                return 2
        fi

        VNC_ARGS=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d " " -f 3-15`

        if [ ! "${USER}" = "root" ] && [ ! "${RUN_AS_USER}" = "${USER}" ] ; then
                echo "You must be root or ${RUN_AS_USER} to check the status of VNC server :${VNCNUM}"
        else
                if [ -f "/home/${RUN_AS_USER}/.vnc/${HOST}:${VNCNUM}.pid" ] ; then
                        echo ":${VNCNUM} running as ${RUN_AS_USER}. PID: `/usr/bin/cat /home/${RUN_AS_USER}/.vnc/${HOST}:${VNCNUM}.pid`"
                else
                        echo ":${VNCNUM} is not running"
                fi
        fi
}

start_all()
{
        for num in `/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -v -P "^\s*#" | /usr/bin/cut -d" " -f1` ; do
                start_number $num
        done
}

stop_all()
{
        for num in `/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -v -P "^\s*#" | /usr/bin/cut -d" " -f1` ; do
                stop_number $num
        done
}

status()
{
        for num in `/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -v -P "^\s*#" | /usr/bin/cut -d" " -f1` ; do               
                status_number $num
        done
}

case "$1" in
'start')
        if [ "$2" = "" ] ; then
                echo "Console number missing"
                exit 2
        fi

        start_number $2
        ;;
       
'start_all')
        start_all
        ;;

'status')
        status
        ;;

'stop_all')
        stop_all
        ;;
       
'stop')
        if [ "$2" = "" ] ; then
                echo "Console number missing"
                exit 2
        fi
        stop_number $2
        ;;
       
*)
        echo "Usage:"
        echo "rc.vncserver start|stop [console_num]"
        echo " -or-"
        echo "rc.vncserver start_all|stop_all|status"
        exit 0
        ;;
esac


mancini 05-14-2005 06:33 AM

the script does not display status nor stop servers properly if they belong to the user root (since his home dir is not under /home)
also it id not work with realvnc because it has a different path fpr vncserver

furthermore it does not work on boot if the user is root and it does not use su , and it does not work on poweroff from the same reasons

i have added VNCSERVER and HOMEDIR to address both these issues and made slight changes to address the others , thanks for a great script arobinson


Code:

#!/bin/sh

# Configuration
#Path for: tightvnc(/usr/local/bin/vncserver) , realvnc(/usr/bin/vncserver)
VNCSERVER=/usr/bin/vncserver
CONF_FILE=/etc/rc.d/rc.vncserver.conf
HOST=`/bin/hostname`

# ensure /usr/X11/bin is in path
PATH=$PATH:/usr/X11/bin

# ensure the user env variable is correct
USER=`/usr/bin/id | /usr/bin/cut -d "(" -f2 | cut -d ")" -f1`

if [ ! -f ${CONF_FILE} ] ; then
        echo "Unable to find configuration file"
        exit 1
fi

start_number()
{
        VNCNUM=$1
        LINE=`/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -P "^${VNCNUM}\b"`
        if [ "$LINE" = "" ] ; then
                echo "No definition found for terminal ${VNCNUM}"
                return 1
        fi

        RUN_AS_USER=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d" " -f2`

        # make sure the user exists
        if ! /usr/bin/grep "${RUN_AS_USER}" /etc/passwd 1> /dev/null ; then
                echo "No such user: ${RUN_AS_USER}"
                return 2
        fi

        VNC_ARGS=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d " " -f 3-15`

        if [ "${RUN_AS_USER}" = "${USER}" ] && [ ! "${USER}" = "root" ] ; then
                ${VNCSERVER} :${VNCNUM} ${VNC_ARGS}
        elif [ "${USER}" = "root" ] ; then
                /bin/su - ${RUN_AS_USER} -c "${VNCSERVER} :${VNCNUM} ${VNC_ARGS}"
        else
                echo "You must be root or ${RUN_AS_USER} to start :${VNCNUM}"
        fi
}

stop_number()
{
        VNCNUM=$1
        LINE=`/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -P "^${VNCNUM}\b"`
        if [ "$LINE" = "" ] ; then
                echo "No definition found for terminal ${VNCNUM}"
                return 1
        fi

        RUN_AS_USER=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d" " -f2`

        # make sure the user exists
        if ! /usr/bin/grep "${RUN_AS_USER}" /etc/passwd 1> /dev/null ; then
                echo "No such user: ${RUN_AS_USER}"
                return 2
        fi

        # define homedir
        if [ "$RUN_AS_USER" = "root" ] ; then
                HOMEDIR=
        else
                HOMEDIR=/home
        fi

        VNC_ARGS=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d " " -f 3-15`

        if [ ! "${USER}" = "root" ] && [ ! "${RUN_AS_USER}" = "${USER}" ] ; then
                echo "You must be root or ${RUN_AS_USER} to stop VNC server :${VNCNUM}"
        else
                if [ -f "${HOMEDIR}/${RUN_AS_USER}/.vnc/${HOST}:${VNCNUM}.pid" ] ; then
                        if [ "${RUN_AS_USER}" = "${USER}" ] && [ ! "${USER}" = "root" ] ; then
                                ${VNCSERVER} -kill :${VNCNUM}
                        else
                                /bin/su - ${RUN_AS_USER} -c "${VNCSERVER} -kill :${VNCNUM}"
                        fi
                fi
        fi
}

status_number()
{
        VNCNUM=$1
        LINE=`/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -P "^${VNCNUM}\b"`
        if [ "$LINE" = "" ] ; then
                echo "No definition found for terminal ${VNCNUM}"
                return 1
        fi

        RUN_AS_USER=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d" " -f2`

        # make sure the user exists
        if ! /usr/bin/grep "${RUN_AS_USER}" /etc/passwd 1> /dev/null ; then
                echo "No such user: ${RUN_AS_USER}"
                return 2
        fi

        # define homedir
        if [ "$RUN_AS_USER" = "root" ] ; then
                HOMEDIR=
        else
                HOMEDIR=/home
        fi

        VNC_ARGS=`/usr/bin/echo "${LINE}" | /usr/bin/cut -d " " -f 3-15`

        if [ ! "${USER}" = "root" ] && [ ! "${RUN_AS_USER}" = "${USER}" ] ; then
                echo "You must be root or ${RUN_AS_USER} to check the status of VNC server :${VNCNUM}"
        else
                if [ -f "${HOMEDIR}/${RUN_AS_USER}/.vnc/${HOST}:${VNCNUM}.pid" ] ; then
                        echo ":${VNCNUM} running as ${RUN_AS_USER}. PID: `/usr/bin/cat ${HOMEDIR}/${RUN_AS_USER}/.vnc/${HOST}:${VNCNUM}.pid`"
                else
                        echo ":${VNCNUM} is not running"
                fi
        fi
}

start_all()
{
        for num in `/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -v -P "^\s*#" | /usr/bin/cut -d" " -f1` ; do
                start_number $num
        done
}

stop_all()
{
        for num in `/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -v -P "^\s*#" | /usr/bin/cut -d" " -f1` ; do
                stop_number $num
        done
}

status()
{
        for num in `/usr/bin/cat ${CONF_FILE} | /usr/bin/grep -v -P "^\s*#" | /usr/bin/cut -d" " -f1` ; do
                status_number $num
        done
}

case "$1" in
'start')
        if [ "$2" = "" ] ; then
                echo "Console number missing"
                exit 2
        fi

        start_number $2
        ;;

'start_all')
        start_all
        ;;

'status')
        status
        ;;

'stop_all')
        stop_all
        ;;

'stop')
        if [ "$2" = "" ] ; then
                echo "Console number missing"
                exit 2
        fi
        stop_number $2
        ;;

*)
        echo "Usage:"
        echo "rc.vncserver start|stop [console_num]"
        echo " -or-"
        echo "rc.vncserver start_all|stop_all|status"
        exit 0
        ;;
esac

also here are some commands for adressing the path issue

Quote:

cp /etc/login.defs /etc/login.defs.old
sed '/^ENV_PATH/s#ENV_.*#&:/usr/X11/bin#' < /etc/login.defs.old > /etc/login.defs
cp /etc/login.defs /etc/login.defs.old
sed '/^ENV_SUPATH/s#ENV_.*#&:/usr/X11/bin#' < /etc/login.defs.old > /etc/login.defs
rm /etc/login.defs.old
and do not forget to add
Quote:

/etc/rc.d/rc.vncserver start_all
to rc.M
and
Quote:

/etc/rc.d/rc.vncserver stop_all
to rc.0


All times are GMT -5. The time now is 08:22 PM.