xorbit / LiFePO4wered-Pi

Access library, command line tool and daemon for the LiFePO4wered/Pi module
GNU General Public License v2.0
132 stars 31 forks source link

Run Script before Shutdown #69

Closed mmoollllee closed 3 months ago

mmoollllee commented 3 months ago

Is there a easy way to send a last log message to a server before shutting down?

xorbit commented 3 months ago

Probably the easiest would be to use something like suggested here:

https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown

This should work whichever way a shutdown is triggered, LiFePO4wered or something else.

mmoollllee commented 3 months ago

Thanks a lot! I came up with the following while handling the SIGTERM signal in the running python script:

[Unit]
Description=Log Shutdown to server
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/bin/sh -c 'cat /home/user/app/.pid | xargs kill'
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target