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?

apfelcast
  • 73
  • 1
  • 3
  • 8

2 Answers2

6

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.

jake
  • 1,347
  • 10
  • 23
  • 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 07 '18 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 07 '18 at 18:54
  • @Ingo You're right, I deleted it. – jake Oct 07 '18 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 07 '18 at 19:25
  • @apfelcast It works at my raspberry. Could you post the output of systemctl cat yourScript.service – jake Oct 07 '18 at 19:44
  • @jake here you go: `# /etc/systemd/system/sendir.service [Unit] Description=sendir

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

    [Install] WantedBy=multi-user.target`

    – apfelcast Oct 07 '18 at 20:37
  • @apfelcast Looks good. I don't know whats wrong there. When I take your service file it works... – jake Oct 07 '18 at 20:51
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.

Dmitry Grigoryev
  • 27,928
  • 6
  • 53
  • 144