Page 1 of 1

The continuing saga of a program auto starting @reboot

Posted: Sat Nov 11, 2017 6:56 pm
by solidago
I have finally managed to get close to having my program start automatically when the pi zero is booted up, but not quite in the way I would find ideal.

I am using the 'systemd' method, which I confess is a little confusing to me, but it only works if the pi config is set to boot up with a monitor and GUI, not CLI.

A monitor does not need to be physically connected but the program does not work without a keyboard attached.

Any suggestions would be very welcomed.

Re: The continuing saga of a program auto starting @reboot

Posted: Sun Nov 12, 2017 3:53 am
by DougieLawson
Stop using cron & stop using rc.local.

Build systemd service files, they're easy.

sudo nano /etc/systemd/system/sunny.service

Code: Select all

[Unit]
Description=Run sunny @ reboot

[Service]
ExecStart=/home/pi/sunny.sh reboot
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=Sunny

[Install]
WantedBy=multi-user.target
sudo systemctl enable sunny.service

If the program runs then terminates add

Code: Select all

Type=oneshot
in the [Service] stanza.

Re: The continuing saga of a program auto starting @reboot

Posted: Sun Nov 12, 2017 8:09 am
by solidago
Thanks Doug

I had already given up on cron and rc.local and attempted the auto start with system.d.

Will try adding that line to the service and see what happens.

Paul

Re: The continuing saga of a program auto starting @reboot

Posted: Sun Nov 12, 2017 11:08 am
by jbudd
Thanks for that example Dougie!
I'm confused by the various ways to start programs automatically, examples are always welcome.

But the setup script I use to install mosquitto creates a file /lib/systemd/system/mosquitto. How come both /lib/systemd/system and /etc/systemd/system exist? What's the difference?

Re: The continuing saga of a program auto starting @reboot

Posted: Sun Nov 12, 2017 12:07 pm
by DougieLawson
/lib/systemd is reserved for apt-get installed packages. /etc/systemd is for user created service files.

Re: The continuing saga of a program auto starting @reboot

Posted: Sun Nov 12, 2017 3:52 pm
by solidago
All working now. Due to the most recent reply from Doug, I converted from having the systemd service file in /lib/ to /etc/ (not sure if that made any difference).

The most recent problem was that the program would only start at boot up if the keyboard was connected. The problem was solved by changing the config to delay the start up until the network had been found.

I am assuming that the start up time took a little longer when the keyboard was attached and that extra time was enough to find the network.

The program couldn't run without a network connection and I suspect this is why the whole thing failed.

My next step must be to arrange a controlled stop in the event of the network connection being lost. It can wait for another day!

Thanks to Doug for the advice. it was much appreciated.

Paul