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
For probably ½ - 1 second.
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.
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/
[Unit] Description=... [Service] Type=oneshot RemainAfterExit=true ExecStop=<your script/program> [Install] WantedBy=multi-user.target
RemainAfterExit=true is needed when you don't have an
After creating the file, make sure to
systemctl daemon-reload and
systemctl enable yourservice --now .
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.
- create the remote ssh access by copying the .pub key of the host (octopi) to the remote ~/.ssh/authorized_keys file to get acces from host to remote.
let ssh user do sudo poweroff without password by use of
sudo visudoto add no password marker for poweroff
add new line underneath with
create the script file with the following line:
ssh -i /home/pi/.ssh/id_rsa -R 1234:localhost:22 -t email@example.com "sudo poweroff"
make it executeable
chmod +x run-before-shutdown.sh
- create the systemd unit from a raspberrypi stackexchange solution below:
[Unit] Description=->start script everytime shutdown gets executed. [Service] Type=oneshot RemainAfterExit=true ExecStart=/bin/true ExecStop=sh /home/pi/run-before-shutdown.sh [Install] WantedBy=multi-user.target
make it active on reboot and start it right away:
systemctl start run-before-shutdown
systemctl enable run-before-shutdown.service
The systemd script does not shut down the remote, while when I do
sh /home/pi/run-before-shutdown.sh directly, the remote terminal closes.
What am I missing?