rricharz
Posts: 78
Joined: Tue Apr 19, 2016 8:46 am
Location: Switzerland
Contact: Website

Poweroff script / systemd [solved]

Sun May 08, 2016 2:58 pm

I want to modify a poweroff script in /etc/system.d to make it work with the newest Raspbian

The script looks like this

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          poweroff
# Required-Start:
# Required-Stop:
# Should-Start:
# Default-Start:
# Default-Stop:      0
# Short-Description: poweroff
# Description:       poweroff
### END INIT INFO

. /lib/lsb/init-functions

case "$1" in
  start|"")
    # Do nothing
    ;;
  restart|reload|force-reload)
    # Do nothing
    ;;
  stop)
    # execute a shell command to initiate power off
    ;;
  status)
    # Do nothing
    ;;
  *)
    # do nothing
    ;;
esac

:
The problem is, that this script gets called after the dist-upgrade with argument $1 being stop even during a reboot. This means that the power is shut off during the reboot process. Reboot is not only broken, but due to the time the hardware needs to react to the power off command (sent via spi), the power might be cut off when the file system is already mounted again.

I cannot find anywhere a simple working example on how to properly migrate this poweroff to systemd.
Last edited by rricharz on Mon May 23, 2016 5:47 am, edited 1 time in total.

rricharz
Posts: 78
Joined: Tue Apr 19, 2016 8:46 am
Location: Switzerland
Contact: Website

Re: Poweroff script / systemd

Tue May 10, 2016 1:32 pm

I tried to make a systemd service, which is executing a program late during the shutdown process and not during a reboot. Is systemd really not able to perform this simple task? 3 days of google, trial and error did not help.

DirkS
Posts: 10304
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Poweroff script / systemd

Tue May 10, 2016 1:38 pm

I want to modify a poweroff script in /etc/system.d to make it work after sudo apt-get dist-upgrade:
I'm struggling to understand why you need a service to boot a pi after a dist-upgrade...

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Poweroff script / systemd

Tue May 10, 2016 2:45 pm

DirkS wrote:I'm struggling to understand why you need a service to boot a pi after a dist-upgrade...
Back in the day, we mowed our own lawns, we fixed our own cars, we did our own dishes. Now the little whipper schnappers need a service for everything! I say, let's return to a time when the kids had to figure out the eigenvalues in the white margins of the Sears and Roebuck catalogue.

rricharz
Posts: 78
Joined: Tue Apr 19, 2016 8:46 am
Location: Switzerland
Contact: Website

Re: Poweroff script / systemd

Tue May 10, 2016 3:47 pm

Let me explain again what I need:

I need a way to execute a program (my_poweroff_program) towards the end of the shutdown process. This program does access the spi bus and turns off the power to the pi. The problem is that after the upgrade to systemd the way this was implemented bevore (see my original post) with system v fails. After upgrading to systemd the program gets called during shutdown, even if a reboot has been initiated.

I have now a mypoweroff.service, which looks as follows:

Code: Select all

[Unit]
Description=mypoweroff.service

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/home/pi/my_poweroff_progam
RemainAfterExit=true
After=????                       (I have not been able to find out what to put here)

[Install]
WantedBy=poweroff.target halt.target

There are two remaining problems with this approach:

1. The service never gets started (of course, because poweroff.target and halt.target are not relevant during startup). I workaround is to call

Code: Select all

sudo systemctl start mypoweroff.service
manually before shutting down the system. If one uses WantedBy=multi-user.target, the service gets started, but then my_poweroff_program gets called during reboot.

2. my_poweroff_program gets called much too early during the shutdown. I have not been able to figure out what I should put into After=????

Thanks for your help

rricharz
Posts: 78
Joined: Tue Apr 19, 2016 8:46 am
Location: Switzerland
Contact: Website

Re: Poweroff script / systemd [solved]

Mon May 23, 2016 5:59 am

I have finally been able to run my poweroff program as late as possible during the shutdown process with the following poweroff.service

Code: Select all

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

[Service]
Type=oneshot
ExecStart=/usr/bin/python /opt/my_poweroff/my_poweroff.py

[Install]
WantedBy=halt.target poweroff.target

Put this service into /lib/systemd/system, and a python script my_poweroff.py to be run at the end of the shutdown process into /opt/my_poweroff/my_poweroff.py

Enable the service with

Code: Select all

cd /lib/systemd/system
sudo systemctl enable poweroff.service

btittelbach
Posts: 1
Joined: Wed Dec 13, 2017 10:56 pm

Re: Poweroff script / systemd [solved]

Wed Dec 13, 2017 11:05 pm

Or even better and simpler, create a file /etc/systemd/system/systemd-poweroff.service.d/mypoweroffscript.conf
with content:

Code: Select all

[Service]
ExecStartPre=/usr/local/bin/my_shutdown_script.sh
Done!.

You may have to create the directory first.

raspnewbie64
Posts: 11
Joined: Fri Nov 30, 2018 6:38 pm

Re: Poweroff script / system for switch on power cord.

Sun Mar 17, 2019 6:27 pm

Hi, I just purchased online via AMAZON a 5v 2.5A power cord. it has a power switch on the cord. I cannot remember where, but back in October time frame i saw a video or write up addressing this cord and someone provided script/code to enhance / utilization of this power switch to assure easier - propper shut down.
Is the above code what I need? If anyone remembers please advise. I am a noobie, lol but want to set up pie correctly from first boot.
also any other recommended code I should add manually please advise.

This pi will be a workstation. I will need to load office apps (like MSOffice/ excel, word, pdf editor, software to complete forms, save and send them etc). I need other business apps so I can create business forms, Purchase Orders, Receipts.
I am disabled and sell vegetables to local stores, also into making/installing Home security devices/linux based and NAS.
(anything to make a dollar to suplement disabilty income to pay bills).

Most humble regards,

Thank you all very much in advance,

Alton

https://www.amazon.com/NorthPada-Raspbe ... way&sr=8-5

pfletch101
Posts: 585
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: Poweroff script / systemd [solved]

Mon Mar 18, 2019 10:20 pm

Please do not resurrect 1+ year old posts, especially when they are not relevant to a new question. The power supply you specify (which may or may not give good service with a Pi - my money is on 'not', at least if you stress the Pi!) has a physical power switch in the 5V supply cable. There is no way on earth that any software running on the Pi will produce a clean shutdown if you turn off its power with such a switch. The thread to which you appended your question was about using an electronically controlled switch to power down a Pi after it had been cleanly shut down.

Return to “Raspberry Pi OS”