Page 1 of 1

How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Fri Feb 08, 2019 9:21 am
by skcasb
Hi All,

I'm using a Raspberry pi 3B+ within Raspian Strecth 9.6.

I need to execute a script that send a command on serial ttyACM0 to switch-off raspberry power-supply, when the command "sudo poweroff" or "sudo halt" are issued.
The source script is visible below:

Code: Select all

[Unit]
Description=Turn power off after shutdown
DefaultDependencies=no
Before=umount.target

[Service]
Type=oneshot
ExecStart=/bin/echo "cmd=switchoff" > /dev/ttyACM0

[Install]
WantedBy=halt.target poweroff.target
I tried to use systemd placing a service script on /lib/systemd/system/mypoweroff.service, enable it using

Code: Select all

systemctl enable mypoweroff.service
but it seems doesn't work.

I googled for a solution, but until now I have not found anything working.

Do you have any suggestions for me ?

Thank you.

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Fri Feb 08, 2019 10:01 am
by tpyo kingg
Shouldn't that ExecStart be ExecStop instead?

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Fri Feb 08, 2019 1:53 pm
by skcasb
I just replaced ExecStart to ExecStop as you suggested but I get the following error when a systemctl status mypoweroff.service issued

Code: Select all

mypoweroff.service: Service has no ExecStart= setting, which is only allowed for RemainAfterExit=y
Do I need to change or add something on service script ?

Thank you.

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Fri Feb 08, 2019 4:53 pm
by tpyo kingg
It would need to have RemainAfterExit also.

Code: Select all

[Service]
RemainAfterExit=yes
Type=oneshot
ExecStart=/bin/echo "cmd=switchoff" > /dev/ttyACM0

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Mon Feb 11, 2019 11:05 am
by skcasb
Added also RemainAfterExit=yes but I still have the issue.

I think the problem is how ExecStart is written. Comparing the rpi-display-backlight.service file with mine is enough to change ExecStart as follows to solve the problem:

Code: Select all

[Unit]
Description=Turn power off after shutdown
DefaultDependencies=no
Before=umount.target

[Service]
Type=oneshot
ExecStart=/bin/sh -c '/bin/echo "cmd=switchoff" > /dev/ttyACM0'

[Install]
WantedBy=halt.target poweroff.target

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Mon Feb 11, 2019 11:15 am
by tpyo kingg
I had an error in the example above. Try the two together:

Code: Select all

[Service]
RemainAfterExit=yes
Type=oneshot
ExecStop=/bin/echo "cmd=switchoff" > /dev/ttyACM0

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Mon Feb 11, 2019 3:44 pm
by skcasb
Hi,

I'm sorry, but using your code I still have this error:

Code: Select all

Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Mon Feb 11, 2019 5:18 pm
by tpyo kingg
Did you reload the systemd services and (re-)start the service once you changed the service file?

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Thu Feb 14, 2019 10:20 am
by skcasb
Yes, also rebooted the raspberry but I still have the same issue using your code.

If I use on "Service" section this code instead of yours

Code: Select all

[Service]
Type=oneshot
ExecStart=/bin/sh -c '/bin/echo "cmd=switchoff" > /dev/ttyACM0'
The service works fine when I issue a poweroff/halt command.

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Thu Feb 14, 2019 12:11 pm
by tpyo kingg
There are probably many ways to get more or less the same result. Based on tests I get the impression that systemd sometimes has inconsistent behavior. Many times it even fails to boot until power cycled.

Anyway, the following are two of the ways I've gotten a script to trigger upon shutdown.

Code: Select all

$ cat /lib/systemd/system/something.service
[[Unit]
Description=Does something upon power off
DefaultDependencies=no

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/sh -c "/bin/date +'0 %T' >> /home/pi/backlight.txt"

[Install]
WantedBy=reboot.target halt.target poweroff.target

Or

Code: Select all

$ cat /lib/systemd/system/something.service
[Unit]
Description=Does something upon power off
DefaultDependencies=no

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/sh -c "/bin/date +'0 %T' >> /home/pi/backlight.txt"

[Install]
WantedBy=multiuser.target
YMMV

Re: How To Execute a Script at Shutdown/Halt on Raspbian Stretch

Posted: Thu Feb 14, 2019 1:26 pm
by skcasb
The first script trigger also on reboot.

I need the service only for halt and shutdown, removing reboot.target from "Wanted by" it works fine.

Thank you.