Ubuntu – How to start VNC server on start-up in Ubuntu 10.04

scriptingstartupUbuntu

I've been struggling with this long enough to ask yet another startup script question.

I've got a startup script that is relatively simple, ie:

### BEGIN INIT INFO
# Provides:          startVNC
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      
# Short-Description: Start VNC server at boot time
# Description:       Test
### END INIT INFO

#!/bin/sh
echo "JOB RUN AT $(date)"
echo "============================"
echo ""
/usr/bin/vncserver -geometry 1280x1024 -depth 24

Obviously it's only the last line that's really important. The script is executable, ie.

ls -l startVNC

yields:

-rwxr-xr-x 1 root root 406 2011-12-07 15:45 startVNC

When I execute it when logged in over ssh as my_user vncserver starts and I am able to see the GUI from my desktop. So then I tried 5 things to make it run on start-up:

  1. Copying my script to /etc/init.d/, calling update-rc.d -f startVNC defaults (also with 99 after defaults to make sure everything vncserver depended on is already running). I checked all the generated symbolic links were there in the /etc/rcX.d/ folders. But it didn't work on reboot or start-up
  2. Adding my script to /etc/rc.local before exit 0
  3. Adding to crontab -e as @reboot /home/my_user/scripts/startVNC
  4. Adding my script to /etc/init.d/rcS
  5. Adding my script to System -> Preferences -> Startup Applications in Gnome

but none of them worked. What more can I check. Is there an error in my script maybe? I guess the process'es owner has to be my_user for it to work, but I have no idea how to debug it.

Any clues appreciated.

Best Answer

You could use su[YourUserName]-c /usr/bin/vncserver to run the vncserver as user, not as root.

Another point: In your script, there's no difference between starting and stopping the VNC server. Usually, a init script would use different cases to start/stop the service:

case "$1" in
    start)
    # code to start the application
    ;;

    stop)
    # code to stop the application
    ;;

    restart)
    $0 stop
    $0 start
    ;;
esac

Here's an elaborated example how to start a vnc server using an init script.

Related Question