How to run a script when print finished and before server shutdown

Hi all,

I'd like to run a command to power off my wifi power socket once the print job is done, or has failed and if I click on "server shutdown" on the UI.
So I've configured some events in the config.yaml and this works like a charm, with one exception, the shutdown event.

As my power off command requires a network connection I assume that the command is run after the network has been brought down.

Can someone confirm this?
Is there something like a pre-shutdown event?

best regards

Stefan

I don't think there is a before shutdown event. It is possible that you have a plugin that is interfering with the shutdown of the octoprint instance, but is highly likely that your network is going down prior to octoprint stopping completely maybe. I'm not sure how those linux dependencies are handled from the service side of octoprint, ie shut down network last.

Full system shutdown doesn't usually allow time for anything extra to run, not like windows' This app is preventing restarting.... IIRC it just forces everything to stop, so there is likely no shutdown event being fired (Linux gurus will probably correct me...). This event is designed for shutdown of the server, not full system - it will run when you run sudo service octoprint stop or similar.

thanks for your replies. any other idea how I can trigger my power off script via the UI?

system actions will add items to the system menu and you can configure it to run your script file. You just have to make sure to use a full path to the script.

1 Like

thanks, this was the missing piece (btw: I've edited my first post for other beginners with the same question)

Now I can power off my wifi socket if
a) an event tells me the print job has finished
b) I select my custom shutdown system action

Just in sense of transperancy, these are my config/script:
config

events:
    enabled: true
    subscriptions:
    -   command: sudo /home/pi/poweroff.sh
        enabled: true
        event:
        - PrintFailed
        - PrintDone
        - PrintCancelled
        type: system
system:
    actions:
    -   action: safepoweroff
        command: sudo /home/pi/poweroff.sh
        confirm: Raspberry Pi will shutdown and power socket will be turned 

off in 45s.
        name: Custom PowerOff

script to power off tasmota driven wifi switch:

#!/bin/sh
curl --location --request -v -s 'http://MYHOST/cm?user=myuser&password=mypass&cmnd=Backlog%20Delay%20450%3BPower%20OFF' >/dev/null
shutdown -h now

I had some trouble using the system actions with sudo, so I allowed to run my poweroff.sh script without any password.

/etc/sudoers

%sudo ALL=(ALL:ALL) NOPASSWD: /home/pi/poweroff.sh

If I had known you were using a Tasmota device I would have said to use my Tasmota plugin. It automates all this.