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.
thanks .
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`
Notes:
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 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.
https://docs.octoprint.org/en/master/events/index.html#gcode-processing
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:
- 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 visudo
to add no password marker for poweroff
add new line underneath withpi ALL=NOPASSWD:/usr/sbin/poweroff
-
create the script file with the following line:
ssh -i /home/pi/.ssh/id_rsa -R 1234:localhost:22 -t pi@octopicam.local "sudo poweroff"
make it executeablechmod +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 daemon-reload
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?