Is it possible to run a script on shutdown after filesystems are unmounted?

Hello,

I try to do something similar already discussed here: Starting Script at Shutdown wont work after Upgrade to OSMC

My raspberry is powered by a wireless socket which I can control with a remote. After a shutdown down, I can use the remote to power off and power on the raspberry again.

I can control any wireless socket also from my raspberry using a 433MHz sender. That works.

What I try to do is to power off the raspberry automatically after shutdown. On shutdown, when the script to power off the socket is run, the raspberry would immediately go off. That could damage the file system I guess.

Would something work like: On the shutdown process, after unmounting the file systems, a script or something should mount the file system again as read-only and call the power-off script. That way it should be safe to power off the raspberry.

What do you think, is that possible? Any suggestions/thoughts?

Thanks in advance.

Casper

Interesting question. I don’t know the exact answer to your question but I think it is possible in principle, so here are some clues:

You would need to find out which systemd service handles remounting the file systems read/write during boot - that same service probably handles remounting it read only during shutdown.

In systemd the shutdown sequence in terms of service ordering is the reverse of startup, so if you want something to shut down after some other service during shutdown you would order it as Before = other service. Because you would need to most likely order it Before = local-fs.target, you would also want DefaultDepencies = no.

Then have a type oneshot service with RemainAfterExit=true, a do-nothing ExecStart (like ExecStart = /bin/true) then run your actual script in ExecStop.

Then at boot it runs the do nothing command, and during shutdown it runs the ExecStop. Remember that with a read only file system your script will be very limited in what it can do.

Sorry I can’t be more specific.

Thank you for your reply. It’s not that easy as I thought. When I have a bit more time, I will look deeper into this. I’ll post the solution, when I found one :o)

1 Like

Update: I’ve tried many things. But I can’t get it to work. I am not able to run a script after the filesystem is read-only, only before.

Finally I found a solution :heart_eyes:

I just had to put my script in /lib/systemd/system-shutdown/.

From the systemd manual:

Immediately before executing the actual system halt/poweroff/reboot/kexec systemd-shutdown will run all executables in /usr/lib/systemd/system-shutdown/ and pass one arguments to them: either “halt”, “poweroff”, “reboot” or “kexec”, depending on the chosen action.

If anyone else is doing similar stuff, here is the script I use:

#!/bin/bash

# only on power-off, not on reboot etc.
if [ "$1" == "poweroff" ]; then
        mount | grep mmcblk0p2 | grep -q ro &&                                          # poor check, if / is really mounted read-only
        /home/funksteckdose/raspberry-remote/send 00000 2 0 > /dev/tty1 2>&1 &&         # turn off socket 2
        /home/funksteckdose/raspberry-remote/send 00000 1 0 > /dev/tty1 2>&1 || {       # turn off socket 1 (raspberry)
                echo "Error: / is NOT read-only or other error occurred!" > /dev/tty1
                echo -en "\n>> " > /dev/tty1
                mount | grep mmcblk0p2 > /dev/tty1
                echo -e "\nNot powering-off. Sleeping 1min.." > /dev/tty1
                sleep 1m
        }
fi

Thanks, i thought about the Same :slight_smile:

Thanks for that. :slight_smile: I was not aware of that system-shutdown folder - that’s really useful to know.