3

I'm trying to run a script right before my Pi shuts down. OS: Raspbian Stretch

Here is what I did so fare:

  1. put my script (name: sendir) in /etc/init.d/
  2. created a symbolic link to it in /etc/rc6.d with the name K99sendir

The script is executable and if I run it inside /etc/rc6.d via ./K99sendir it works but it does not work when I actually shutdown the system.

This is what the script looks like:

#!/bin/bash

irsend SEND_ONCE ph_remote KEY_POWER

Does anybody have an idea what's missing?

1
  • Just as a side note, rc6 is for reboot, rc0 is for shutdown. Not that I expect it to work if you replace 6 with 0. Oct 19, 2018 at 8:18

2 Answers 2

7

If you are using systemd, just put your script to /lib/systemd/system-shutdown/ as described here and here.

EDIT: If it's not working try this one:

Create a systemd service: /etc/systemd/system/yourSript.service with following lines:

[Unit]
Description=yourScript

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/local/bin/yourScript

[Install]
WantedBy=multi-user.target

Move your Script to /usr/local/bin/yourScript.

And enable the service

sudo systemctl daemon-reload

sudo systemctl enable yourScript.service --now.

7
  • Actually I tried this method before trying so use the run levels. But for me it also didn't work with the systemd method. Do I really only have to copy the script in this directory or do I have to adjust the script?
    – apfelcast
    Oct 7, 2018 at 18:09
  • Nice idea :-) But for starting this unit you do not need any dependency. It can start as soon as possible. I think you could omit RequiresMountsFor=/. And I won't place it in /lib/systemd/system/. Better to use /etc/systemd/system/.
    – Ingo
    Oct 7, 2018 at 18:54
  • @Ingo You're right, I deleted it.
    – jake
    Oct 7, 2018 at 18:58
  • Thank you for the details. I created the service according to your description, but after enabling it, I got the following error: Executing: /lib/systemd/systemd-sysv-install enable sendir update-rc.d: error: sendir Default-Start contains no runlevels, aborting.
    – apfelcast
    Oct 7, 2018 at 19:25
  • @apfelcast It works at my raspberry. Could you post the output of systemctl cat yourScript.service
    – jake
    Oct 7, 2018 at 19:44
2

The reason why your script isn't working is that apparently, starting with Jessie, Debian breaks compatibility with System V scripts. They still seem to work correctly on startup (their most important use case), but no longer do on shutdown.

You'll have to get used to systemd.

Your Answer

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

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