solidago
Posts: 8
Joined: Wed Sep 13, 2017 6:49 pm

The continuing saga of a program auto starting @reboot

Sat Nov 11, 2017 6:56 pm

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.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

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

Sun Nov 12, 2017 3:53 am

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

solidago
Posts: 8
Joined: Wed Sep 13, 2017 6:49 pm

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

Sun Nov 12, 2017 8:09 am

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

jbudd
Posts: 1446
Joined: Mon Dec 16, 2013 10:23 am

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

Sun Nov 12, 2017 11:08 am

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?

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

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

Sun Nov 12, 2017 12:07 pm

/lib/systemd is reserved for apt-get installed packages. /etc/systemd is for user created service files.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

solidago
Posts: 8
Joined: Wed Sep 13, 2017 6:49 pm

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

Sun Nov 12, 2017 3:52 pm

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

Return to “Beginners”