nigeljohnson73
Posts: 9
Joined: Wed Nov 20, 2019 11:59 pm

shutdown script will not change the status of the LED

Thu Jan 09, 2020 7:11 am

I have setup a script that is supposed to run on shutdown as a service. It simply echo's 'heartbeat' to the LED file so the overlay picks it up and makes it blink. I can run the script manually and it works fine. If I 'stop' the service, which I am led to believe mimics the shutdown behaviour, the script will not run. If I actually do a reboot or shutdown, the script does not run. I have seen that during the shutdown, filesystems are unmounted, so that may be why it might not work on the actual reboot and I should possibly remount the root FS to do the echo, however doing a stop command on the service does not switch the FS to read only, and it still won't run then.

Oh, for completness, the overlary in /boot/config.txt sets the led up as heartbeat and a script that runs when startup completes sets it to default-on. The shutdown bit is therefore designed to switch it back to heartbeat.

What am I missing? Is it true that the old rc.6 and init.d stuff is being removed?

Here are a few commands to show what I've done:

Code: Select all

[email protected]:~ $ cat /etc/systemd/system/heartbeat_indicator.service
[Unit]
Description=This service sets the indicator LED into the heartbeat mode on shutdown.

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/webroot/gStation/sh/heartbeat_indicator.sh

[Install]
WantedBy=multi-user.target
[email protected]:~ $ sudo systemctl daemon-reload
[email protected]:~ $ sudo systemctl enable heartbeat_indicator

# just in case this is supposed to be done here after the enable
[email protected]:~ $ sudo systemctl daemon-reload 
[email protected]:~ $ sudo systemctl start heartbeat_indicator --now 
# no errors reported

# mimic shutdown??
[email protected]:~ $ sudo systemctl stop heartbeat_indicator
[email protected]:~ $ 

#Nothing happened, look in the log files
[email protected]:~ $ cd /var/log
[email protected]:/var/log $ grep "Switching LED to heartbeat mode" *
grep: apache2: Is a directory
grep: apt: Is a directory
grep: btmp: Permission denied
grep: mysql: Is a directory
grep: private: Permission denied
[email protected]:~ $ 

[email protected]:~ $ cat /webroot/gStation/sh/heartbeat_indicator.sh
#/bin/bash

echo "Switching LED to heartbeat mode"
echo "heartbeat" | sudo tee /sys/class/leds/led0/trigger >/dev/null

# run it maually as the pi user
[email protected]:~ $ /webroot/gStation/sh/heartbeat_indicator.sh 
Switching LED to heartbeat mode
[email protected]:~ $ 

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2585
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: shutdown script will not change the status of the LED

Thu Jan 09, 2020 9:11 am

[ This isn't a Device Tree question, but it does at least refer to overlays so I'll allow it. ;) ]

What does "sudo systemctl status heartbeat_indicator" report before and after the service is stopped?

nigeljohnson73
Posts: 9
Joined: Wed Nov 20, 2019 11:59 pm

Re: shutdown script will not change the status of the LED

Thu Jan 09, 2020 6:50 pm

That's where I was kind of going :)

Did a reboot, then this:

Code: Select all

[email protected]:~ $ sudo systemctl status heartbeat_indicator
● heartbeat_indicator.service - This service sets the indicator LED into the heartbeat mode on shutdown.
   Loaded: loaded (/webroot/gStation/res/heartbeat_indicator.service; enabled; vendor preset: enabled)
   Active: active (exited) since Thu 2020-01-09 18:47:58 GMT; 45s ago

Jan 09 18:47:58 gcam systemd[1]: Started This service sets the indicator LED into the heartbeat mode on shutdown..
[email protected]:~ $ date
Thu  9 Jan 18:48:49 GMT 2020
[email protected]:~ $ 

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2585
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: shutdown script will not change the status of the LED

Thu Jan 09, 2020 8:07 pm

My guess would be that because systemd knows the service has exited, it won't try to stop it.

It ought to be possible to get actions performed at shutdown in a more direct way, but right now I couldn't say what that way is.

nigeljohnson73
Posts: 9
Joined: Wed Nov 20, 2019 11:59 pm

Re: shutdown script will not change the status of the LED

Fri Jan 10, 2020 7:03 am

Back in the olden days, you could use runtime level scripts, (Silicon Graphis workstations, solaris etc) I think that's possibly still the case with the rc.0-6 directories in /etc, but I keep seeing that this is not the way to go and that systemd should be used. is the runlevel stuff being 'phased out' ?

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2585
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: shutdown script will not change the status of the LED

Fri Jan 10, 2020 9:52 am

After reading around a bit, I think this might work (but I've not tried):

Code: Select all

[Unit]
Description=This service sets the indicator LED into the heartbeat mode on shutdown.
DefaultDependencies=no
After=final.target

[Service]
Type=oneshot
ExecStart=/webroot/gStation/sh/heartbeat_indicator.sh

[Install]
WantedBy=final.target

nigeljohnson73
Posts: 9
Joined: Wed Nov 20, 2019 11:59 pm

Re: shutdown script will not change the status of the LED

Sat Jan 11, 2020 8:50 am

Nope, that's not the one :)

After a reboot, I run this:

Code: Select all

[email protected]:~ $ sudo systemctl status heartbeat_indicator
● heartbeat_indicator.service - This service sets the indicator LED into the heartbeat mode on shutdown.
   Loaded: loaded (/webroot/gStation/res/heartbeat_indicator.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2020-01-11 08:41:45 GMT; 28s ago
  Process: 101 ExecStart=/webroot/gStation/sh/heartbeat_indicator.sh (code=exited, status=203/EXEC)
 Main PID: 101 (code=exited, status=203/EXEC)

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
[email protected]:~ $ 
And when I reboot or shutdown, the light does not flash.

I also added the ExecStop line and that didn't work. I also tried an ExecStart=/bin/true, but still nothing.

If this is supposed to replace the old working run-level system this needs to step up a notch :(

nigeljohnson73
Posts: 9
Joined: Wed Nov 20, 2019 11:59 pm

Re: shutdown script will not change the status of the LED

Sat Jan 11, 2020 1:14 pm

Ok, I think I found something that makes a bit of sense. If I change the ExecStart or ExecStop to have sh /webroot/... etc... things work from the commandline... but the light does not flash regardless of how I start the script (stop at the end of multi-user.target or starts at reboot.target or poweroff.target)

Might now be a filesystem unmounted thing, and I might just need to put some dependancies. Happier now :)

Thanks for the help PhilE :)

nigeljohnson73
Posts: 9
Joined: Wed Nov 20, 2019 11:59 pm

Re: shutdown script will not change the status of the LED

Sat Jan 11, 2020 1:35 pm

Turns out it was nothing to do with permissions... It just needed the full path to tee in the script

Code: Select all

echo "heartbeat" | sudo /usr/bin/tee /sys/class/leds/led0/trigger >/dev/null

so the service file looks like this now:

Code: Select all

[Unit]
Description=LED heartbeat

[Service]
ExecStop=sh /webroot/gStation/sh/heartbeat_indicator.sh
Type=oneshot
RemainAfterExit=yes


[Install]
WantedBy=multi-user.target

I couldn't make the echo command work in the service file directly, but that's a battle for the optimisation part of the process :D

Return to “Device Tree”