3

I've enabled the following systemd unit file in order to properly shutdown my Raspberry Pi before a real timer in between cuts the power off. but it does a reboot, not the actual shutdown that I want. Any idea?

systemd-poweroff.timer

[Unit]
Description=Poweroff every work day
# Call necessary service
Unit=systemd-poweroff.service

[Timer]
OnCalendar=Mon,Tue,Wed,Thu,Fri *-*-* 00:00:00
Persistent=false

[Install]
WantedBy=timers.target

Distributor ID: Raspbian

Description: Raspbian GNU/Linux 9.11 (stretch)

Release: 9.11/ Codename: stretch Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux

2
  • Perhaps you should just try to halt rather than "poweroff" which the pi cannot do as such. Try a timer for systemd-halt.service.
    – meuh
    May 25, 2020 at 16:01
  • There is a note here linux.org/docs/man8/systemd-shutdown.html that says never use that service but to use systemctl halt I ended up having a python program responding to mqtt and the scheduled task pushing the mqtt command!
    – user115418
    May 25, 2020 at 18:21

1 Answer 1

5

First of all: you are running Raspbian Stretch but tagged the question with pi-4. This is not possible. A Raspberry Pi 4B can only run Raspbian Buster.

Your timer Unit looks good and should do. But you define some default settings that are not necessary. This normally should not do any harm, but having problems it's always a good idea to configure with defaults. I have tested it with Raspbian Buster Light.

A systemd timer Unit calls by default a service of the same name, so a timer Unit named systemd-poweroff.timer will call a service of systemd-poweroff.service if not specified otherwise. So first check if this service is working as expected:

rpi ~$ sudo systemctl start systemd-poweroff.service

Then the last messages on my screen are:

--- snip ---
[  OK  ] Reached target Shutdown.
[  OK  ] Reached target Final Step.
[  OK  ] Started Power-Off.
[399078.812158] reboot: Power down

I have to powercycle to restart the RasPi.

Then I created the timer with:

rpi ~$ sudo systemctl --force --full edit systemd-poweroff.timer

In the empty editor I inserted these statements, saved them and quit the editor:

[Unit]
Description=Poweroff every work day

[Timer]
#OnCalendar=Mon,Tue,Wed,Thu,Fri *-*-* 00:00:00
OnBootSec=180

[Install]
WantedBy=timers.target

For testing I used OnBootSec=180, which will poweroff the RasPi 3 minutes after bootup. Enable it now will poweroff the RasPi immediately because 3 minutes are already gone since last bootup.

rpi ~$ sudo systemctl enable --now systemd-poweroff.timer

This works as expected. I have to powercycle to restart the RasPi.

Check and stop the poweroff timer. You have 3 minutes to do it ;-)

rpi ~$ systemctl list-timers systemd-poweroff.timer
NEXT                         LEFT         LAST PASSED UNIT                   ACTIVATES
Tue 2020-05-26 12:40:28 BST  1min 5s left n/a  n/a    systemd-poweroff.timer systemd-poweroff.service

1 timers listed.

rpi ~$ sudo systemctl disable --now systemd-poweroff.timer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.