Page 1 of 1

Script at shutdown not working

Posted: Sun Jun 28, 2020 4:38 pm
by Cr4z33
I am trying to setup a script when shooting down (only) my Pi 4 B.

This is the tvoff.sh script I put in /home/pi/

Code: Select all

echo standby 0 | cec-client -s -d 1


and this is the turn-tv-off-at-shutdown.service file

Code: Select all

[Unit]
Description=TUrn off the TV at shutdown only
DefaultDependencies=no
Conflicts=reboot.target
Before=poweroff.target halt.target shutdown.target
Requires=poweroff.target

[Service]
Type=oneshot
ExecStart=/home/pi/tvoff.sh start
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target


It simply doesn't turn off the TV at shutdown so what is wrong here? :?

Re: Script at shutdown not working

Posted: Sun Jun 28, 2020 6:34 pm
by bls
Per https://www.freedesktop.org/software/sy ... rvice.html, ExecStart specifies the command to run when the service is started. But, you want the script to run when shutting down, right? In that case, you should be using ExecStop.

If I've misunderstood what you're trying to do, please explain the scenario and your goals in more detail.

Also, don't know what cec-client is, but you may have to jigger around when your service is actually stopped, since cec-client may require other system resources that have already been shut down by the time tvoff.sh is run.

Re: Script at shutdown not working

Posted: Sun Jun 28, 2020 6:48 pm
by Cr4z33
bls wrote:
Sun Jun 28, 2020 6:34 pm
Per https://www.freedesktop.org/software/sy ... rvice.html, ExecStart specifies the command to run when the service is started. But, you want the script to run when shutting down, right? In that case, you should be using ExecStop.
That's probably the reason thanks. :)

Gonna try tomorrow morning to see if it works.

Re: Script at shutdown not working

Posted: Mon Jun 29, 2020 3:35 pm
by Cr4z33
Nope it didn't work. :cry:

Re: Script at shutdown not working

Posted: Tue Jun 30, 2020 7:56 am
by RonR
@Cr4z33,

First, create two files:

/home/pi/tvoff:

Code: Select all

#!/bin/bash

if [ $(systemctl list-jobs | grep -c -e "reboot.target.*start") -eq 0 ]; then
  echo standby 0 | cec-client -s -d 1
fi

/home/pi/tvoff.service:

Code: Select all

[Unit]
Description=Turn off TV at halt/shutdown (not reboot)
Before=shutdown.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStop=/home/pi/tvoff

[Install]
WantedBy=multi-user.target

Then run:

Code: Select all

chmod +x /home/pi/tvoff
sudo ln -s /home/pi/tvoff.service /etc/systemd/system/tvoff.service
sudo systemctl enable tvoff
sudo systemctl start tvoff

Re: Script at shutdown not working

Posted: Tue Jun 30, 2020 1:21 pm
by Cr4z33
@RonR it worked like a charm thank you! :mrgreen: