skcasb
Posts: 13
Joined: Wed Jul 11, 2018 12:46 pm

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

Fri Feb 08, 2019 9:21 am

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.

tpyo kingg
Posts: 591
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

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

Fri Feb 08, 2019 10:01 am

Shouldn't that ExecStart be ExecStop instead?

skcasb
Posts: 13
Joined: Wed Jul 11, 2018 12:46 pm

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

Fri Feb 08, 2019 1:53 pm

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.

tpyo kingg
Posts: 591
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

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

Fri Feb 08, 2019 4:53 pm

It would need to have RemainAfterExit also.

Code: Select all

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

skcasb
Posts: 13
Joined: Wed Jul 11, 2018 12:46 pm

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

Mon Feb 11, 2019 11:05 am

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

tpyo kingg
Posts: 591
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

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

Mon Feb 11, 2019 11:15 am

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

skcasb
Posts: 13
Joined: Wed Jul 11, 2018 12:46 pm

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

Mon Feb 11, 2019 3:44 pm

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.

tpyo kingg
Posts: 591
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

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

Mon Feb 11, 2019 5:18 pm

Did you reload the systemd services and (re-)start the service once you changed the service file?

skcasb
Posts: 13
Joined: Wed Jul 11, 2018 12:46 pm

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

Thu Feb 14, 2019 10:20 am

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.

tpyo kingg
Posts: 591
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

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

Thu Feb 14, 2019 12:11 pm

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

skcasb
Posts: 13
Joined: Wed Jul 11, 2018 12:46 pm

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

Thu Feb 14, 2019 1:26 pm

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.

Return to “Beginners”