Monday, 20 August 2012

Start VNC automatically

So a couple of you have been asking about getting VNC to start automatically (me included).  I did a bit of digging and have figured out what to do - it's a few simple steps.  First off though, must say thanks to Penguin Tutor for the actual answer to this issue.


So - I'm assuming that you have VNC up and running, working if you start it manually after each boot.  If not, take a look here.


Create startup file
The first thing you need to do is create the file that will actually start VNC on startup.  I'm doing this via Putty (instructions here), but you can just as easily use Terminal directly from the Pi.

Type the following:
sudo nano /etc/init.d/tightvncserver

NOTE: nano is a lightweight editor - the command above will create a new file called tightvncserver in the /etc/init.d directory and open it for editing, presenting the following screen (note the [ New File ] entry at the bottom, indicating a new file):

Once you have the file open, paste in the following:

# First configure the user you want to run this under - this will generally be pi, unless you've created your own users
export USER='pi'

eval cd ~$USER

# Check the state of the command - this'll either be start or stop 
case "$1" in
  start)
    # if it's start, then start vncserver using the details below
    su $USER -c '/usr/bin/vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565'
    echo "Starting vncserver for $USER "
    ;;
  stop)
    # if it's stop, then just kill the process
    pkill Xtightvnc
    echo "vncserver stopped"
    ;;
  *)
    echo "Usage: /etc/init.d/vncserver {start|stop}"
    exit 1
    ;;
esac
exit 0

# thanks - check out http://myraspberrypiexperience.blogspot.co.uk for more info!


NOTE: In the file I've specified my own resolution with the text -geometry 1280x800 - you can leave this out of you want

This will be what your screen now looks like.

To exit, press Ctrl+X.  You'll be prompted to save - just type Y:

You'll be prompted to confirm the filename - just press Enter:


Set file properties
Once that's done make sure the file has the right propertied by typing the following:
sudo chmod 755 /etc/init.d/tightvncserver

NOTE: Important to do this step every time you modify this file.  I redid these steps to create the tutorial, skipped this step and couldn't VNC in on the reboot!

Add File to startup sequence
And now just add your file to the list of startup actions by typing the following:
sudo update-rc.d tightvncserver defaults

And that should be it!  Just restart, and you should be able to VNC straight in!

51 comments:

  1. I need help. My server did not start after a reboot, no matter what script i use. I´m using Raspbian “wheezy” from 2012-08-16. I did all the steps described above. I am wondering about your note to the chmod, but i did it. Can you help me?

    ReplyDelete
    Replies
    1. Hi Bernhard - I'm not too sure I'm afraid. The first time I tried, it also didn't work, but after I ran the chmod step it worked. I deleted the file, re-traced my steps a 2nd time and it worked that time too.

      Best advice? Scrap everything and try it again from scratch, re-run through the steps & instructions :) So often I kick myself when it works the 2nd time.

      Something else I tried when troubleshooting my initial issue was to connect via Putty and run the file you created manually - that'll identify if it's an issue with the file or configuring it in the start-up process.

      Hope this helps!

      Delete
  2. In the tightvncserver file you use different names when starting and killing the process, why is that?

    ReplyDelete
  3. If I understand it correctly, Xtightvnc in the pkill command is the name or the process that is actually running, whereas the application itself is called vncserver. I haven't confirmed this yet, but if you get vnc up and running and then look at your processes you should see one running called Xtightvnc.

    ReplyDelete
  4. My own fault totally but any way you can *prevent* vnc from loading after doing this?

    I'm pretty new with Linux and currently cant figure out how to get onto my desktop now!!

    Probably something really obvious but Id appreciate a pointer.

    ReplyDelete
    Replies
    1. I haven't tried to use this myself, but after a quick google it looks like you'll just need to do the following:
      1) Delete the script file you created
      2) Remove the script from the startup process

      As mentioned, I haven't tested this myself, but I think the commands would be as follows:
      1) sudo delete /etc/init.d/tightvncserver
      2) sudo update-rc.d tightvncserver remove

      I hope it works!

      Delete
    2. hi tass,

      i think you made a small mistake in the commands, delete should be rm (stands for remove), no shame on you becous i had to look it up ;) just letting every1 know.

      Harrie

      Delete
    3. Thanks for the head-up Harrie!

      Delete
  5. Good day to all
    I follow with interest and passion raspberry on the blog, I tried to start the tightvncserver to boot, that does not work, the error 'this (update-rc.d: using dependency based boot sequencing
    insserv: warning: script 'tightvncserver' missing LSB tags and overrides
    ).
    There will be 'a solution to the problem?
    thanks
    Stefano

    ReplyDelete
    Replies
    1. Hi Stefano. I haven't actually hit that problem on my Pi, although I've tried to replicate this same process on my Ubuntu machine and got pretty much the same issue. Not too sure where it comes from. I see someone has posted an alternative below that might help. What he's done there is to put the actual command line straight into the rc file, rather than using the method above which places a shortcut of sorts into the rc file. This direct approach might get better results?

      Delete
  6. You can actually have this start at a boot-up by editing your /etc/rc.local file and inserting the following before the exit 0 line.

    su -c "tightvncserver :1 -geometry 1280x1024 -depth 24" user

    Geometry you can change to your screen resolution and user would your username that you use to login. I have seen vncserver be used instead of tightvncserver but couldn't get that to work on my system.

    ReplyDelete
    Replies
    1. Thanks for the suggestion - hope this more direct approach helps Stefano out!

      Delete
  7. I was also getting "Missing LSB tags and overrides."

    Adding the following to the start of the file seemed to take care of it:
    ### BEGIN INIT INFO
    # Provides: vncserver
    # Required-Start: networking
    # Required-Stop:
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Starts VNC
    # Description:
    ### END INIT INFO

    No warnings, and I was able to VNC in without a problem.

    ReplyDelete
  8. Hi,

    It seems that tightvnc server stops responding after a day or so. I have it running on my pi which is always on. Any ideas why this happens and what a possible resolution is?

    Thanks

    ReplyDelete
    Replies
    1. Sorry samad64, I haven't had this problem as I don't tend to leave my Pi on long enough, and if I do it's to use Raspbmc. I'll try get one of my other boards up and running soon to see if I experience the same issue. What distro and you running?

      Delete
  9. im using raspbian wheezey from 10/28/12

    thanks for looking into it

    ReplyDelete
    Replies
    1. Right - I've re-imaged, am up and running - let's see how it goes. I've got a few more questions:
      - how are you starting VNC - automatically or manually?
      - if manually, what command are you using?
      - if automatically are you using the initial command I posted, or have you tried the 'Unknown' comment above?

      Delete
    2. I'm using your automatic method. Have not tried unknown's comment above.

      I've got a workaround for now, a cron job set to execute every 24 hours to kill the server and restart it... but its not very ideal

      When I am unable to connect, it simply says the password is incorrect - though a restart of vnc will let me in with the same password

      Delete
    3. OK, thanks. Odd that it's giving you a password error. I've started with the other approach first (su -c "tightvncserver :1 -geometry 1280x1024 -depth 24" user). The poster suggested tighvncserver rather than vncserver as he had issues with vncserver - might be related. I'll keep it running over the next few days and keep connecting in and let you know how it goes.

      Delete
    4. Hi samad64. So I've run this for the past few days, intermittently connecting, without any issue. There are a couple of things different that I can think of - it might be worth trying them one by one:
      - I'm using the latest Wheezy release (2012-12-16). You might not have to create a new blank image - simply running sudo apt-get update
      - I've running tightvncserver now rather than vncserver like my post outlines.

      I don't think it's hardware related due to the 'password' error, so I would hope of those 2 sort it out. Let us know how it goes.

      Delete
  10. Adding:

    su -c "tightvncserver :1 -geometry 1280x1024 -depth 24" user


    to the /etc/rc.local file, did solve the missing LSB tags and overrides error.

    Thanks for the excellent guide!

    ReplyDelete
  11. You can actually have this start at a boot-up by editing your /etc/rc.local file and inserting the following before the exit 0 line.
    su -c "tightvncserver :1 -geometry 1280x1024 -depth 24" user
    Regard's,
    Ben Linus,
    Go here for more info

    ReplyDelete
  12. Unfortunately my pi didn't start the vnc server after the reboot. What is the problem. This is a virgin pi, I have been using it since 2 hours. :)

    ReplyDelete
  13. hello all,
    If you have problems with starting vnc on boot you can have a look on this website:

    http://learn.adafruit.com/adafruit-raspberry-pi-lesson-7-remote-control-with-vnc/running-vncserver-at-startup

    For me it worked out this way :D
    Good luck!

    ReplyDelete
    Replies
    1. first i want to say that i am glad to help you with my google skills :P

      This method is also more useful in another way, becous it will only start the vnc server when startx (desktop) get launched so it will not try to start it up on boot and fail (if you are not starting startx automatically) and then you still need to start it manually..

      gr. Harrie

      Delete
  14. hi ,
    i have a problem when running vnc server, like this :

    Couldn't start Xtightvnc; trying default font path.
    Please set correct fontPath in the vncserver script.
    Couldn't start Xtightvnc process.

    help me please

    ReplyDelete
  15. Every tutorial I have used to get tightvncserver to start at boot renders my pi useless. After doing this one I am stuck in a login boot - A box asking me for my username pi, and my password raspberry with a choice of default login. Password works but then just comes back up again.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Raspberry Pi... Teach your kids about computing! I was a unix admin long ago (now a windows admin). Linux has had 20+ years to evolve and we're still dealing with this obtuse command-line crap. What a joke!

    ReplyDelete
  18. Great job and very nice content... i read this blog, this blog is very helpful for me...
    I am very thankful to share this post.. I hope you have more information about this post.. So, Please share me.. Thanks..
    Visit my site:- Linksys Router Support

    ReplyDelete
  19. Wow its really nice service provider to solve your problem in bootmgr missing error go through this link and solve your any query about windows 7 realted problem .
    BOOTMGR Missing in Windows 7
    Thank you
    Aalia lyon

    ReplyDelete
  20. Gusto mo ba ng online work?
    Earn while you enjoy time with your Family & Lovedones!
    Email me: tirada_uno_23@yahoo.com / tiradauno@gmail.com / n.helphelphelp@gmail.com
    Contact Number: 09465046302
    For more details, kindly visit http://www.unemployedpinoys.com/

    ReplyDelete
  21. Great post!
    I also have a Pi blog: http://raspberrynewbie.wordpress.com/

    ReplyDelete
  22. Now that you have the ability to VNC into your Pi, you will likely never want to hook it up to a monitor, mouse and keyboard again.

    ReplyDelete
  23. woww artikel yg sangat sangat bagus sekali..

    Agen Bola Terpercaya

    ReplyDelete
  24. Hi, I've been looking through the web to find a solution to my vnc or x problem. I can successfully connect to my pi via vnc however all I get is grey desktop and x mouse pointer. Any ideas why? Cheers, George

    ReplyDelete
  25. i was using headless setup using ethernet, but suddenly it has stopped working due to ethernet port, . is there any way to setup headless via VNC directly without display ?

    ReplyDelete
  26. i was using headless setup using ethernet, but suddenly it has stopped working due to ethernet port, . is there any way to setup headless via VNC directly without display ?

    ReplyDelete
  27. If you are looking for space in East Delhi , just logon www.think4buysale.in and make it perfect deal for yourself as www.think4buysale.in is the best destination to find property deals in patparganj industrial area with real pics of premises and will complete solution to your requirements.Just logon to think4buysale.in or call on +919958023001 and let the experts do the best for you.

    Office space for rent in patparganj Industrial area

    Office space for rent in karkardooma

    Commercial Office space for rent in laxmi nagar

    Office space for rent in Preet vihar


    Bridal Mehndi Artist in Delhi

    ReplyDelete
  28. https://www.completegate.com/2016080659/blog/raspberry-pi-the-ultimate-convenience Do read and share your views ....

    ReplyDelete
  29. I think, this is really very well information given about Marriage Places List in Delhi - Wedding Hall List in Delhi. Thanks for given this information here about this blog.
    3bhk Flats in Velachery

    ReplyDelete
  30. We are providing you affordable and fully furnished flats in ideal location of Delhi, from where all the prime location and significant area are only few minutes drive away.
    1BHK Flats for Rent in Pune

    ReplyDelete