Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
3  
Note that your shebang (the line that starts #!) has no effect, when it's not the first line of the file it's just another comment. –  Kowh Dec 8 '11 at 0:06
    
Another good point, thanks. –  mmm Dec 9 '11 at 11:33
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
Thanks a lot tohuwawohu. This helped me to find errors in my script. I'll post it as another answer in about 8 hours, because my reputation doesn't allow me to do it just now (darn it). –  mmm Dec 7 '11 at 17:00
add comment

OK, I got it. The problem was:

  1. Running my script as root, which normally would happen, won't work. That's why it's necessary to use su [my_user] -c
  2. One needs to use quotation marks for the options, ie.

    su my_user -c "/usr/bin/vncserver -geometry 1280x1024 -depth 16"

  3. I also added . /lib/lsb/init-functions and exit 0 - don't know if those are necessary, but for sure they don't do any harm. Just copied those from the elaborate example provided by tohuwawohu.

So I ended up with:

### 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:       A sophisticated script to do the job.
### END INIT INFO

#!/bin/sh
echo "JOB RUN AT $(date)"
echo "============================"
echo ""

. /lib/lsb/init-functions
su my_user -c "/usr/bin/vncserver -geometry 1280x1024 -depth 16"

exit 0

Which works just fine. Of course, using the above cited elaborate script might be even better. I added the script to start-up via:

sudo update-rc.d -f startVNC start 99 2 3 4 5 .

because I don't see any reason to run anything on shutdown (beyond logging, which I'm not doing).

Also see the comment about the shebang (the line that starts #!) being misplaced here under my original question. It works, though, so I didn't want to include untested code here. You should change it probably if you'd like to use it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.