Execute comandline after shudown called

Can I somehow execute a command after clicking shutdown in octoprint ui but before my pi actually has shutdown?

If you're quick enough - yes :smiley:
For probably ½ - 1 second.

thanks :grinning_face_with_smiling_eyes:.

I meant that different. Second try:

Does there exist a hook to execute a CL command "always" before my pi shuts down?
Or a Plugin that can do that?

To execute a script at shutdown or reboot:

save your script in `/etc/rc6.d`
Make it executable: `sudo chmod +x K99_script`


The script in rc6.d must be with no .sh extension
The name of your script must begin with K99 to run at the right time.
The scripts in this directory are executed in alphabetical order.


1 Like

and there are several ways to accomplish it with systemd
You would need a script that is to run and a service definition which you save into /etc/systemd/sytem/


ExecStop=<your script/program>


RemainAfterExit=true is needed when you don't have an ExecStart action.

After creating the file, make sure to systemctl daemon-reload and systemctl enable yourservice --now .

(linux - How to run a script with systemd right before shutdown? - Unix & Linux Stack Exchange)

Thanks for the sample code.

Just to be sure.
There is an Event Manager built in to Octoprint that has PowerOff as Event and System commands as Type.
Can I not send an ssh command like in the Print screen when I shut down octoprint?

I have a public certificate of this remote device on my octoprint host an I can log in over ssh from it.
But it does not react.

The Power off event is not for the server, it's for the M81 command which would power off the printer (if supported). It won't work for OctoPrint shutdown.


not really sure from which system you send this to which other system. For me it often pays to try those ssh commands from a shell first, often I get a confirmation dialog on the first go which never finishes unless I interact.
There is a comprehensive collection of ways to stop or restart a running linux system

Ah. I see.
So then, I think the systemd version is good for me.
Can you help me sort out the right constraints? I think I have to go sure the network does not disconnect before I send the command. Anything else I have to look out for?

Edit: deleted dumb answer.

I tested another few things.
After reading this about systemd reboot and the dependent link here, I did the following steps:

  1. copy the .pub key of the host (octopi) to the remote ~/.ssh/authorized_keys file to get acces from host to remote.
  2. use sudo visudo to add no password marker for poweroff
    add new line underneath with pi ALL=NOPASSWD:/usr/sbin/poweroff
  3. create file: run-before-shutdown.sh with the following line saved in it:
    ssh -i /home/pi/.ssh/id_rsa -R 1234:localhost:22 -t pi@octopicam.local "sudo poweroff"
    make it executeable chmod +x run-before-shutdown.sh
  4. create the systemd unit from a raspberrypi stackexchange solution:
Description=->start script everytime shutdown gets executed.

ExecStop=sh /home/pi/run-before-shutdown.sh

  1. make it active on reboot and start it right away:
    systemctl daemon-reload
    systemctl start run-before-shutdown
    systemctl enable run-before-shutdown.service

Test with open ssh terminal of remote if it quits when I do sudo shutdown now.
But it does not shutdown the remote, while when i do sh /home/pi/run-before-shutdown.sh directly the remote terminal closes.

What am I missing?