PDA

View Full Version : [SOLVED] Autorun x0vnc4server after user login



CrewDK
April 12th, 2016, 09:18 PM
Hey. I'm running Xubuntu 14.04 with installed vnc4server. In my /etc/init.d/ i created script which starts x0vnc4server.


#!/bin/sh
### BEGIN INIT INFO
# Provides: vnc0
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: vnc4launcher
# Description:
### END INIT INFO

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"

USER=`ps aux | grep xfce4-session | grep -v grep | awk '{print $1}'`
ADDR=`ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | cut -d: -f2 | awk '{printf $1"\n"}'`

# The display that VNC will use
DISPLAY="0"

OPTIONS="-display :${DISPLAY} -PasswordFile ~/.vnc/passwd"

. /lib/lsb/init-functions

case "$1" in

start)
log_action_begin_msg "Starting vncserver for user '${USER}' on ${ADDR}:${DISPLAY}"
su ${USER} -c "x0vnc4server ${OPTIONS} &"
;;

stop)
log_action_begin_msg "Stoping vncserver for user '${USER}' on ${ADDR}:${DISPLAY}"
killall x0vnc4server
;;

restart)
$0 stop
$0 start
;;
*)
echo "Usage: /etc/init.d/vnc4 {start|stop|restart}"
exit 1
esac

exit 0

If i running it manualy after user login - it works like charm. But is there any way to autorun this script after user login into XFCE?

CrewDK
April 13th, 2016, 11:36 PM
Nvm. Working solution. This script installs and sets up vnc4 server with autostart right services with or without user logon, and with opportunity to connect to remote host with dispaly :1+ and with display :0.


#!/bin/bash

############################
## Переменные

PASS=12345678
#USER=crew

###########################
## Удаление и установка

apt-get purge vino -y
apt-get install vnc4server -y

##########################
## Раскидываем конфиги и пароли на всех пользователей

for h in `ls /home | grep -v "lost+found" | grep -v iso | grep -v grep`;
do
mkdir -p /home/${h}/.vnc/
echo -e "${PASS}\n${PASS}\n\n" | vncpasswd /home/${h}/.vnc/passwd 2>&1 > /dev/null

cat <<- "EOF" > /home/${h}/.vnc/xstartup
#!/bin/bash
# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
#xsetroot -solid grey
#vncconfig -iconic &
#xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &
EOF

chmod +x /home/${h}/.vnc/xstartup
chown -R ${h}: /home/${h}/.vnc/
done

##########################
## Копируем в рута и в скелет
cp -R /home/crew/.vnc/ /root/.vnc
cp -R /home/crew/.vnc/ /etc/skel/.vnc


##########################
## Делаем общедоступным рабочий скрипт
chmod 777 /etc/X11/xinit/xinitrc

#########################
## Создаём скрипты для запуска и автозапуска

#####################
# для display:1+


cat <<- "EOF" > /etc/init.d/vnc4
#!/bin/sh
### BEGIN INIT INFO
# Provides: vnc4
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: vnc4launcher
# Description:
### END INIT INFO

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
# Addres
ADDR=`ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | cut -d: -f2 | awk '{printf $1"\n"}'`

# The Username:Group that will run VNC
export USER="crew"

# The display that VNC will use
DISPLAY="1"

# Color depth (between 8 and 32)
DEPTH="16"

# The Desktop geometry to use.
#GEOMETRY="x"
#GEOMETRY="800x600"
GEOMETRY="1200x900"
#GEOMETRY="1280x1024"

# The name that the VNC Desktop will have.
NAME="vnc4server"

OPTIONS="-name '${NAME} from ${USER}' -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"

. /lib/lsb/init-functions

case "$1" in

start)
log_action_begin_msg "Starting vncserver for user '${USER}' on ${ADDR}:${DISPLAY}"
su ${USER} -c "vnc4server ${OPTIONS}"
;;

stop)
log_action_begin_msg "Stoping vncserver for user '${USER}' on ${ADDR}:${DISPLAY}"
su ${USER} -c "vnc4server -kill :${DISPLAY}"
# vnc4server -kill :${DISPLAY}
;;

restart)
$0 stop
$0 start
;;
*)
echo "Usage: /etc/init.d/vnc4 {start|stop|restart}"
exit 1
esac

exit 0
EOF

chmod +x /etc/init.d/vnc4

update-rc.d vnc4 start 70 2 3 4 5 . stop 20 0 1 6 .

#####################
# для display 0:

cat <<- "EOF" > /etc/init.d/vnc0
### BEGIN INIT INFO
# Provides: vnc0
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: vnc4launcher
# Description:
### END INIT INFO

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"

USER=`ps aux | grep xfce4-session | grep -v grep | awk '{print $1}'`
ADDR=`ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | cut -d: -f2 | awk '{printf $1"\n"}'`

# The display that VNC will use
DISPLAY="0"

OPTIONS="-display :${DISPLAY}"

. /lib/lsb/init-functions

case "$1" in

start)
sleep 5
log_action_begin_msg "Starting vncserver for user '${USER}' on ${ADDR}:${DISPLAY}"
su ${USER} -c "x0vnc4server ${OPTIONS} -PasswordFile ~/.vnc/passwd &"
sleep 5
ps aux | grep x0vnc4 | grep -v grep
if [ $? -ne 0 ]; then
x0vnc4server ${OPTIONS} -PasswordFile ${HOME}/.vnc/passwd &
fi
;;

stop)
log_action_begin_msg "Stoping vncserver for user '${USER}' on ${ADDR}:${DISPLAY}"
killall x0vnc4server
;;

restart)
$0 stop
$0 start
;;
*)
echo "Usage: /etc/init.d/vnc0 {start|stop|restart}"
exit 1
esac

exit 0
EOF

chmod +x /etc/init.d/vnc0

##############################
## Раскидываем в хомяки пользователей

for HM in `ls -R -a /home/ | grep autostart: | grep -v grep | rev | cut -c 2- | rev`;
do
cd ${HM}
OWN=`ls -l .. | grep autostart | grep -v grep | awk '{print $3}'`
cat <<- EOF > ${HM}/vnc0.desktop
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=vnc0
Comment=
Exec=/etc/init.d/vnc0 start
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false
EOF

chown ${OWN}: ${HM}/vnc0.desktop
done


100% works for Xubuntu 14.04. IDK about other DE.