I am running my 3D printer via DSF on a Raspberry PI. Its like Octoprint for RRF/Duet Boards. After a print is finished you can call the M81 command to poweroff the printer (Assuming you have installed the ATX Power Off). But this do not work on the PI. So found this Plugin: https://github.com/wilriker/execonmcode
This will parse the DSF for a specific M Command you can choose! The standard one is M7722.
It then runs whatever you want on the PI. For installing the plugin and installing it as a service i made some small YT Videos!