ALM865
Posts: 9
Joined: Sat Oct 13, 2012 2:34 am

Issues with Script

Sat Dec 09, 2017 9:46 am

Hi All,

I have a service that I've created that worked fine on wheezy but I've just updated to the lastest RPi image and it no longer works.

My script is "/etc/init.d/vncboot" and looks like this:

Code: Select all

### BEGIN INIT INFO
# Provides: vncboot
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start VNC Server at boot time
# Description: Start VNC Server at boot time.
### END INIT INFO

#! /bin/sh
# /etc/init.d/vncboot

USER=root
HOME=/root

export USER HOME

case "$1" in
 start)
   echo "Starting VNC Server"
   #Insert your favoured settings for a VNC session
   #/usr/bin/vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565
   /usr/bin/vncserver :1 -geometry 1920x1080 -depth 24
   ;;

 stop)
   echo "Stopping VNC Server"
   /usr/bin/vncserver -kill :1
   ;;

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

exit 0
Then as root I run "chmod 755 /etc/init.d/vncboot", "chmod +x /etc/init.d/vncboot" and "update-rc.d vncboot defaults"

I have no idea what's wrong, if I run the script as is it works flawlessly. If I run it using 'service vncboot start' it fails. See below:

Code: Select all

root@rpi:/home/pi# service vncboot start
Job for vncboot.service failed because the control process exited with error code.
See "systemctl status vncboot.service" and "journalctl -xe" for details.
root@rpi:/home/pi# systemctl status vncboot.service
● vncboot.service - LSB: Start VNC Server at boot time
   Loaded: loaded (/etc/init.d/vncboot; generated; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2017-12-09 20:39:47 AEDT; 11s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1152 ExecStart=/etc/init.d/vncboot start (code=exited, status=203/EXEC)

Dec 09 20:39:47 rpi systemd[1]: Starting LSB: Start VNC Server at boot time...
Dec 09 20:39:47 rpi systemd[1]: vncboot.service: Control process exited, code=exited status=203
Dec 09 20:39:47 rpi systemd[1]: Failed to start LSB: Start VNC Server at boot time.
Dec 09 20:39:47 rpi systemd[1]: vncboot.service: Unit entered failed state.
Dec 09 20:39:47 rpi systemd[1]: vncboot.service: Failed with result 'exit-code'.
root@rpi:/home/pi# /etc/init.d/vncboot start
Starting VNC Server

New 'X' desktop is rpi:1

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/rpi:1.log

Does anyone know what's going on here?

ALM865
Posts: 9
Joined: Sat Oct 13, 2012 2:34 am

Re: Issues with Script

Sat Dec 09, 2017 11:31 am

So added ". /lib/lsb/init-functions" and moved "#! /bin/sh" to the start and it now works.

Pretty annoying that it worked fine on wheezy though.

NotRequired
Posts: 412
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Issues with Script

Sat Dec 09, 2017 12:29 pm

On Wheezy the SysV style init scripts was how services was started but since Jessie this has been taken over by SystemD. Even though init scripts work fine on >= Jessie, you should (if possible) create a systemd service instead. The "shebang" line (#! ...) must be the first line in a shell script in order to work properly :)
Glowing in the dark!

Heater
Posts: 19683
Joined: Tue Jul 17, 2012 3:02 pm

Re: Issues with Script

Sat Dec 09, 2017 6:16 pm

The modern way to get things started is to use systemd. It's pretty simple.

Create a systemd service file, "vnc.service", like so:

Code: Select all

    [Unit]
    Description= VNC Server
    After=network.target

    [Service]
    ExecStart=/usr/bin/vncserver :1 -geometry 1920x1080 -depth 24
    WorkingDirectory=/root
    StandardOutput=syslog
    StandardError=syslog
    SyslogIdentifier=vncserver
    User=root
    Group=root
    Restart=always

    [Install]
    WantedBy=multi-user.target
Put it in the right place with right permissions

Code: Select all

    $ sudo cp vnc.service /etc/systemd/system 
Now you can start the service like so:

Code: Select all

    $ sudo systemctl start vnc
Stop it like so:

Code: Select all

    $ sudo systemctl stop vnc
Enable it to run at boot time:

Code: Select all

    $ sudo systemctl enable vnc
Disable it from running at boot time:

Code: Select all

    $ sudo systemctl disable vnc
Lot's of people complain about systemd for whatever reasons but I have been using it on Pi, PC and cloud servers for ages and it has worked very well. I find it easier than messing with a bunch of init scripts.

No need for any "shebang" line :)
Slava Ukrayini.

ALM865
Posts: 9
Joined: Sat Oct 13, 2012 2:34 am

Re: Issues with Script

Sun Dec 10, 2017 1:56 am

Thanks for the replies.

@NotRequired, I'm not sure why the script has been fine with the shebang line after the init info all this time. I've used the init script fine for many years until yesterday when my SD got shagged and I had to do a complete rebuild of my Pi setup. I'm guessing it was always documented that way and never enforced until recently?

The Pi box isn't doing much these days, it just sits there until I have something big I want to download or download something off a slow server so I don't have to leave my laptop on. It sits there idling 99.9% of the time.

@Heater, yeah I did see the new way using systemd when I was googling but I wasn't 100% sure how to "convert" my init.d script. Thanks for the example, I'll give it a go at some point out of interest. It does look a fair bit easier than an init.d script.

Heater
Posts: 19683
Joined: Tue Jul 17, 2012 3:02 pm

Re: Issues with Script

Sun Dec 10, 2017 7:59 am

If you have a bash script without the shebabg line (hash bang bin bash) you can run it as a bash script using the bash command:

$ bash myscript

If you have the hash bang bin bash then you can run it:

$ ./myscript

In the latter case the hash bang bin bash line tells your shell which interpreter to use to run the script (There are many command line shells in Unix/Linux)

I'm guessing this is the difference between running your script from the old and new init schemes.
Slava Ukrayini.

NotRequired
Posts: 412
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Issues with Script

Sun Dec 10, 2017 9:45 am

ALM865 wrote:
Sun Dec 10, 2017 1:56 am
I'm guessing it was always documented that way and never enforced until recently?
This is pure speculation: Linux can detect content type / mime type of most files by examining a part of the files content. If your file with the ill placed shebang is detected as a shell script, it may function properly as a such with older versions of Linux or even from a command line in newer versions. SystemD may disregard the mime-type and purely rely on the shebang because this is faster than analyzing file content and this difference may be the cause of your "problem" (in fact you script has always been malformed so it was just dumb luck it worked).

I hope you get your Pi up & running as you want to! :)
Glowing in the dark!

Heater
Posts: 19683
Joined: Tue Jul 17, 2012 3:02 pm

Re: Issues with Script

Sun Dec 10, 2017 10:25 am

That's right.

The "file" command does a very good job of identifying file types.

Code: Select all

$ file certs/ca.crt
certs/ca.crt: PEM certificate
Even without the extension:

Code: Select all

$ file ca
ca: PEM certificate
But it needs the #!/bin/sh to identify shell scripts.

Code: Select all

$ cat junk1
echo Hello
$ file1 junk
junk: ASCII text
$ cat junk2
#!/bin/bash
echo Hello
$ file junk2
junk: Bourne-Again shell script, ASCII text executable
Slava Ukrayini.

Return to “General discussion”