0

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?

  • 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. – Dmitry Grigoryev Oct 19 '18 at 8:18
3

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 /user/local/bin/yourScript.

And enable the service

sudo systemctl daemon-reload

sudo systemctl enable yourScript.service --now.

  • 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 '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 7 '18 at 18:54
  • @Ingo You're right, I deleted it. – jake Oct 7 '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 7 '18 at 19:25
  • @apfelcast It works at my raspberry. Could you post the output of systemctl cat yourScript.service – jake Oct 7 '18 at 19:44
0

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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