LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 10-29-2004, 11:28 AM   #1
arobinson74
Member
 
Registered: Nov 2002
Location: Lone Tree, CO
Distribution: Xubuntu Gutsy
Posts: 174

Rep: Reputation: 30
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
 
Old 10-29-2004, 11:32 AM   #2
arobinson74
Member
 
Registered: Nov 2002
Location: Lone Tree, CO
Distribution: Xubuntu Gutsy
Posts: 174

Original Poster
Rep: Reputation: 30
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
 
Old 10-29-2004, 11:34 AM   #3
arobinson74
Member
 
Registered: Nov 2002
Location: Lone Tree, CO
Distribution: Xubuntu Gutsy
Posts: 174

Original Poster
Rep: Reputation: 30
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)
 
Old 10-29-2004, 12:20 PM   #4
arobinson74
Member
 
Registered: Nov 2002
Location: Lone Tree, CO
Distribution: Xubuntu Gutsy
Posts: 174

Original Poster
Rep: Reputation: 30
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
 
Old 05-14-2005, 05:33 AM   #5
mancini
LQ Newbie
 
Registered: Sep 2004
Distribution: Slackware
Posts: 16

Rep: Reputation: 0
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

Last edited by mancini; 05-14-2005 at 09:50 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vncserver on startup greenmeanie *BSD 1 10-22-2005 11:16 AM
how to startup the vncserver ust Linux - Distributions 2 05-25-2005 07:58 AM
vncserver File, don't have one. Maxplayer14 Mandriva 3 02-24-2005 03:43 PM
Starting a vncserver instance automatically at startup paul.nel Linux - General 6 01-08-2004 08:24 PM
VncServer - Startup issues TrueSword Linux - General 1 12-06-2002 05:13 PM


All times are GMT -5. The time now is 10:35 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration