tennisparty
Posts: 13
Joined: Tue Sep 24, 2019 12:36 pm

Systemd - script running on boot and shutdown

Mon Mar 23, 2020 8:59 pm

Hello,

I have created a systemd service which runs a python script just after boot. I am having a problem that the python script is running a second time when the pi starts shutting down. Pi Model3A+ running the most recent version of Buster. I am using the python code to calculate dusk and dawn times so that the project is only on when it is dark (between sunset and sunrise - I am using a mopi to trigger shutdown/ boot times).

My systemd file looks like this -

Code: Select all

[Unit]
Description=astralatstart
After=time-sync.target

[Service]
ExecStart=/usr/bin/python3 -u astralcalc.py
WorkingDirectory=/home/pi/Desktop
StandardOutput=append:/home/pi/Desktop/xastralcalc.log
StandardError=inherit
Restart=no

[Install]
WantedBy=multi-user.target
My project is dependent on an RTC (DS 3231) to keep time - the project will be setup in a forest well away from Wifi or a data connection so this will count time. I setup the RTC based on this guide - modifying slightly to allow for the fact I am running Buster https://spellfoundry.com/sleepy-pi/sett ... an-jessie/

Is there a reason that the script is re-running on shutdown and is there a way for me to add an additional dependency in the systemd service file so that my python script only runs at startup?

Many thanks,
Sam

Jim Danner
Posts: 6
Joined: Sun Nov 17, 2019 5:10 pm

Re: Systemd - script running on boot and shutdown

Wed Mar 25, 2020 12:46 pm

How have you set up the running at boot? Normally you'd have an [Install] section with an entry

Code: Select all

WantedBy=...
and then the command

Code: Select all

sudo systemctl enable astralatstart
would create a symlink to your unit file in the right place. Have you created that symlink yourself? Where?

DarkElvenAngel
Posts: 420
Joined: Tue Mar 20, 2018 9:53 pm

Re: Systemd - script running on boot and shutdown

Wed Mar 25, 2020 1:13 pm

tennisparty wrote:
Mon Mar 23, 2020 8:59 pm
Hello,

I have created a systemd service which runs a python script just after boot. I am having a problem that the python script is running a second time when the pi starts shutting down. Pi Model3A+ running the most recent version of Buster. I am using the python code to calculate dusk and dawn times so that the project is only on when it is dark (between sunset and sunrise - I am using a mopi to trigger shutdown/ boot times).

My systemd file looks like this -

Code: Select all

[Unit]
Description=astralatstart
After=time-sync.target

[Service]
ExecStart=/usr/bin/python3 -u astralcalc.py
WorkingDirectory=/home/pi/Desktop
StandardOutput=append:/home/pi/Desktop/xastralcalc.log
StandardError=inherit
Restart=no

[Install]
WantedBy=multi-user.target
My project is dependent on an RTC (DS 3231) to keep time - the project will be setup in a forest well away from Wifi or a data connection so this will count time. I setup the RTC based on this guide - modifying slightly to allow for the fact I am running Buster https://spellfoundry.com/sleepy-pi/sett ... an-jessie/

Is there a reason that the script is re-running on shutdown and is there a way for me to add an additional dependency in the systemd service file so that my python script only runs at startup?

Many thanks,
Sam
I'm throwing a theory here but it might be after time-sync.target that's doing it.

Since your pi will synchronization time again at shutdown. This could be wrong

tennisparty
Posts: 13
Joined: Tue Sep 24, 2019 12:36 pm

Re: Systemd - script running on boot and shutdown

Wed Mar 25, 2020 9:35 pm

Jim Danner wrote:
Wed Mar 25, 2020 12:46 pm
How have you set up the running at boot? Normally you'd have an [Install] section with an entry

Code: Select all

WantedBy=...
and then the command

Code: Select all

sudo systemctl enable astralatstart
would create a symlink to your unit file in the right place. Have you created that symlink yourself? Where?
Hi Jim I have run

Code: Select all

sudo systemctl enable astralatstart
following this https://www.raspberrypi.org/documentati ... systemd.md
The systemd service file is saved in /etc/systemd/system

Would it be possible to explain how I would add an install section in a little more detail, or an appropriate target in the wantedby line, I don't quite catch your drift,

Sorry I'm quite new to systemd

Thanks for your help,
Sam

tennisparty
Posts: 13
Joined: Tue Sep 24, 2019 12:36 pm

Re: Systemd - script running on boot and shutdown

Wed Mar 25, 2020 9:36 pm

DarkElvenAngel wrote:
Wed Mar 25, 2020 1:13 pm
tennisparty wrote:
Mon Mar 23, 2020 8:59 pm
Hello,

I have created a systemd service which runs a python script just after boot. I am having a problem that the python script is running a second time when the pi starts shutting down. Pi Model3A+ running the most recent version of Buster. I am using the python code to calculate dusk and dawn times so that the project is only on when it is dark (between sunset and sunrise - I am using a mopi to trigger shutdown/ boot times).

My systemd file looks like this -

Code: Select all

[Unit]
Description=astralatstart
After=time-sync.target

[Service]
ExecStart=/usr/bin/python3 -u astralcalc.py
WorkingDirectory=/home/pi/Desktop
StandardOutput=append:/home/pi/Desktop/xastralcalc.log
StandardError=inherit
Restart=no

[Install]
WantedBy=multi-user.target
My project is dependent on an RTC (DS 3231) to keep time - the project will be setup in a forest well away from Wifi or a data connection so this will count time. I setup the RTC based on this guide - modifying slightly to allow for the fact I am running Buster https://spellfoundry.com/sleepy-pi/sett ... an-jessie/

Is there a reason that the script is re-running on shutdown and is there a way for me to add an additional dependency in the systemd service file so that my python script only runs at startup?

Many thanks,
Sam
I'm throwing a theory here but it might be after time-sync.target that's doing it.

Since your pi will synchronization time again at shutdown. This could be wrong
[/quote

My thoughts exactly I was hoping I could add a second dependency which means the code runs after the time sync but only on boot

DarkElvenAngel
Posts: 420
Joined: Tue Mar 20, 2018 9:53 pm

Re: Systemd - script running on boot and shutdown

Wed Mar 25, 2020 10:12 pm

tennisparty wrote:
Wed Mar 25, 2020 9:36 pm
DarkElvenAngel wrote:
Wed Mar 25, 2020 1:13 pm
tennisparty wrote:
Mon Mar 23, 2020 8:59 pm
Hello,

I have created a systemd service which runs a python script just after boot. I am having a problem that the python script is running a second time when the pi starts shutting down. Pi Model3A+ running the most recent version of Buster. I am using the python code to calculate dusk and dawn times so that the project is only on when it is dark (between sunset and sunrise - I am using a mopi to trigger shutdown/ boot times).

My systemd file looks like this -

Code: Select all

[Unit]
Description=astralatstart
After=time-sync.target

[Service]
ExecStart=/usr/bin/python3 -u astralcalc.py
WorkingDirectory=/home/pi/Desktop
StandardOutput=append:/home/pi/Desktop/xastralcalc.log
StandardError=inherit
Restart=no

[Install]
WantedBy=multi-user.target
My project is dependent on an RTC (DS 3231) to keep time - the project will be setup in a forest well away from Wifi or a data connection so this will count time. I setup the RTC based on this guide - modifying slightly to allow for the fact I am running Buster https://spellfoundry.com/sleepy-pi/sett ... an-jessie/

Is there a reason that the script is re-running on shutdown and is there a way for me to add an additional dependency in the systemd service file so that my python script only runs at startup?

Many thanks,
Sam
I'm throwing a theory here but it might be after time-sync.target that's doing it.

Since your pi will synchronization time again at shutdown. This could be wrong
My thoughts exactly I was hoping I could add a second dependency which means the code runs after the time sync but only on boot
Try

Code: Select all

After=basic.target

bls
Posts: 454
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: Systemd - script running on boot and shutdown

Thu Mar 26, 2020 12:17 am

Maybe systemd thinks that running ExecStart is the right thing to do on shutdown, but I didn't see anything in the systemd.service documentation indicating that a service's ExecStart would get called again on system shutdown.

Have you tried adding something like ExecStop=/bin/true ?

tennisparty
Posts: 13
Joined: Tue Sep 24, 2019 12:36 pm

Re: Systemd - script running on boot and shutdown

Thu Mar 26, 2020 8:57 am

bls wrote:
Thu Mar 26, 2020 12:17 am
Maybe systemd thinks that running ExecStart is the right thing to do on shutdown, but I didn't see anything in the systemd.service documentation indicating that a service's ExecStart would get called again on system shutdown.

Have you tried adding something like ExecStop=/bin/true ?
Hi, just so I understand what will that do, the docs say the following
ExecStart
The commands and arguments executed when the service starts.

ExecStartPre, ExecStartPost
Additional commands that are executed before or after the command in ExecStart.

ExecReload
The commands and arguments to execute when the service reloads.

ExecStop
The commands and arguments to execute when the service stops.

ExecStopPost
Additional commands to execute after the service stops.
I don't think I need a command executed after my original python file, or is it creating a file to say that it has already run like
RemainAfterExit
A boolean value that specifies whether the service shall be considered active even if all its processes exited. Defaults to no
Thanks again,
Sam

Jim Danner
Posts: 6
Joined: Sun Nov 17, 2019 5:10 pm

Re: Systemd - script running on boot and shutdown

Fri Mar 27, 2020 1:46 pm

tennisparty wrote:
Wed Mar 25, 2020 9:35 pm
Hi Jim I have run

Code: Select all

sudo systemctl enable astralatstart
following this https://www.raspberrypi.org/documentati ... systemd.md
The systemd service file is saved in /etc/systemd/system
The command systemctl enable can only work on a unit file that has an [Install] section. I just tried running the command on your unit file from your first post, and the answer I get is

Code: Select all

The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
So I don't understand how you got your service to run automatically at all.

Are you sure you haven't created some symlink to the unit file somewhere?

DarkElvenAngel
Posts: 420
Joined: Tue Mar 20, 2018 9:53 pm

Re: Systemd - script running on boot and shutdown

Fri Mar 27, 2020 3:30 pm

Jim Danner wrote:
Fri Mar 27, 2020 1:46 pm
tennisparty wrote:
Wed Mar 25, 2020 9:35 pm
Hi Jim I have run

Code: Select all

sudo systemctl enable astralatstart
following this https://www.raspberrypi.org/documentati ... systemd.md
The systemd service file is saved in /etc/systemd/system
The command systemctl enable can only work on a unit file that has an [Install] section. I just tried running the command on your unit file from your first post, and the answer I get is

Code: Select all

The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
So I don't understand how you got your service to run automatically at all.

Are you sure you haven't created some symlink to the unit file somewhere?
There is an install section scroll the code window

Code: Select all

[Install]
WantedBy=multi-user.target
It's there so you are chasing a bad copy paste

tennisparty
Posts: 13
Joined: Tue Sep 24, 2019 12:36 pm

Re: Systemd - script running on boot and shutdown

Sat Mar 28, 2020 11:49 am

Hello, thanks for your help, some useful thoughts. Unfortunately I tried them but none worked. I have realised its probably an error on my part when describing the problem. I still need some advice. I had assumed the pi was rerunning the script on shutdown but I think it is actually running the script on startup, once before the rtc (or network) syncs the time, then it is rerunning the script when the time is synced (by the rtc or network).

The first time my code runs it thinks the time is still the same as when the pi shutdowns. The second time it runs the time is correctly updated.

Does anyone have an idea why the original after=time-sync dependency would cause this behaviour. I am going to try changing the systemd script to

Code: Select all

... 
After=basic.target
... 
[Service]
Type=idle 
... 
Here's hoping,
Sam

Jim Danner
Posts: 6
Joined: Sun Nov 17, 2019 5:10 pm

Re: Systemd - script running on boot and shutdown

Tue Mar 31, 2020 12:33 pm

It seems that systemd is acting in unexpected ways again (what a surprise!).

As discussed here, the time-sync.target
does not help much because this is only for initializing the time-sync service. After intitializing it needs some time to connect to the time server and get the clock synchronized to it.
In other words, your service will run after the time-synchronization service has started, but by then it hasn't actually got the right time yet.

The same discussion provides the solution, at least for systemd past version 239 (check this with systemctl --version ): you specify After=time-sync.target as you did, but you must also have the service systemd-time-wait-sync enabled on your system. This is done with

Code: Select all

sudo systemctl enable systemd-time-wait-sync
If that post is correct, it should then work.

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

Re: Systemd - script running on boot and shutdown

Tue Mar 31, 2020 2:22 pm

Jim Danner wrote:
Tue Mar 31, 2020 12:33 pm
It seems that systemd is acting in unexpected ways again (what a surprise!).

As discussed here, the time-sync.target
does not help much because this is only for initializing the time-sync service. After intitializing it needs some time to connect to the time server and get the clock synchronized to it.
In other words, your service will run after the time-synchronization service has started, but by then it hasn't actually got the right time yet.

The same discussion provides the solution, at least for systemd past version 239 (check this with systemctl --version ): you specify After=time-sync.target as you did, but you must also have the service systemd-time-wait-sync enabled on your system. This is done with

Code: Select all

sudo systemctl enable systemd-time-wait-sync
If that post is correct, it should then work.

Have you been running BRANCH=next rpi-update? There's a critical Linux system timer bug in that.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

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

Jim Danner
Posts: 6
Joined: Sun Nov 17, 2019 5:10 pm

Re: Systemd - script running on boot and shutdown

Tue Mar 31, 2020 3:45 pm

DougieLawson wrote:
Tue Mar 31, 2020 2:22 pm
Have you been running BRANCH=next rpi-update? There's a critical Linux system timer bug in that.
Are you asking the OP? I'm just quoting from a thread on StackExchange, which says this is a general systemd quirk, not a problem of any particular version.

Return to “Raspbian”