Starting Script at Shutdown wont work after Upgrade to OSMC

Hello,

I have been using Raspbmc for a while now and just ungraded to OSMC in order be able to use a hifiberry digi, this, however, caused problems with my setup.

My Pi B+ is connected to a projector and for sound output to a stereo. As I like to use sleep in front of the TV, I installed a 433Mhz-sender, that would switch off the stereo (through a 433Mhz controlled power plug) once the Pi was shut down through a timer… This worked perfectly fine with Raspbmc, but stopped working after setting it up on OSMC.

I installed the 433Mhz-software following the following instruction:

As it is in German, I briefly summarize what I did:

sudo apt-get update
sudo apt-get install git-core
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build:
cd ~
git clone git://github.com/xkonni/raspberry-remote.git
cd raspberry-remote
make send

In order to make “make” work, I had to install gcc, g++ and make itself.

To this point all worked perfectly fine and I am able to switch my power pug using the following command:

/home/osmc/raspberry-remote/send 00001 1 0

In order to make my Pi use that script at shutdown, I did the following, which worked on Pasrbmc but not on OSMC.

I created a script called “sendremote”, which looks like this:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          Schaltet die Steckdose ab.
# Required-Start:    
# Required-Stop:     
# Default-Start:     
# Default-Stop:      0 1 6
# Short-Description: Kurze Beschreibung
# Description:       Längere Bechreibung
### END INIT INFO
# Author: Name <email@domain.tld>

# Aktionen
#
case "$1" in
    start)
        /home/osmc/raspberry-remote/send 00001 1 0
        ;;
    stop)
        /home/osmc/raspberry-remote/send 00001 1 0
        ;;
    restart)
        /home/osmc/raspberry-remote/send 00001 1 0
        ;;
esac
#
exit 0 

I copied this script to /etc/init.d

sudo cp sendremote /etc/init.d

Then I made it 755

sudo chmod 755 /etc/init.d/sendremote

And then I used the following command to make this script stop and runlevel 0, 1 and 6

update-rc.d tc_un_mount stop 02 0 1 6

This tutorial was taken from this German website and, as I said, worked for Raspbmc.
https://debianforum.de/forum/viewtopic.php?f=34&t=123447

It does not work on OSMC however. When using the last command I got this back:

update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults 

I looked into the /etc/rcX.d folders of the runlevels I copied it to and in all of those I found “K01sendremote” but somehow the script is not been used that shutdown, it does not work…

Can somebody please help e to make it work?
Thanks a lot!

1 Like

OSMC uses systemd for the init system so is only partially compatible with legacy init.d scripts through a backward compatibility layer.

You’re much better off writing a native systemd unit. You can find some documentation here:

http://www.freedesktop.org/software/systemd/man/systemd.service.html

The trick is to write a service unit that will run a command during shutdown, which isn’t as obvious as it appears.

Something like this may work:

[Unit]
Description=433Mhz sender
Before=multi-user.target
Conflicts=shutdown.target

[Service]
ExecStart=/bin/true
ExecStop=/home/osmc/raspberry-remote/send 00001 1 0
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Make this file be called sendremote.service and place it in /etc/systemd/system/ then enable it with:

sudo systemctl enable sendremote.service

The trick here is that we create a service which “starts” during boot but actually does nothing (you can try it without the ExecStart line and I think it will still work) and “conflicts” with shutdown.target. What this does is that when you go to shutdown or restart systemd will try to move to the state shutdown.target at which point all services which declare a conflict with shutdown.target will be shut down.

As part of this shut down the ExecStop command will be called. Simple. :smile:

Although I don’t think you need to in this particular case, if you need to do something during shutdown that must happen before the network connection is taken down, you would do so by putting

After=network.target

In the unit section. This works because systemd performs shut down in the reverse order of startup, so declaring that we must start after network.target (network service - in our case connman) also declares that we must be shut down before the network service is stopped…

2 Likes

Thanks man! You are a genius, sir! I tried so many times getting it right and spend so any hours, nearly threw that pi out of the window and now I just add this little thingy and it works! Thanks for th quick help…

Am I assuming correctly that if I changed

ExecStart=/bin/true

to

ExecStart=/home/osmc/raspberry-remote/send 00001 1 1

that this would turn on the controlled power plug (the 1 at the end changes the sender to turn the plug on awhereas a 0 at the end turns it off)?

So this makes it plug turn on at boot and off at shutdown?

ExecStart=/home/osmc/raspberry-remote/send 00001 1 1
ExecStop=/home/osmc/raspberry-remote/send 00001 1 0

Thanks a lot for the quick help! You are awesome!

Yes that should work - ExecStart will run during startup, although it will run fairly late in the startup process, around the time that Kodi is starting. You could change the Before line to make it start before Kodi, like so:

Before=multi-user.target mediacenter.service
1 Like