Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 5962

Perform a task on Linux shutdown

$
0
0

I want to run a .service that "hears" a shutdown signal (preferably both manual, with physical buttons, and with sudo shutdown/reboot etc) and when it does hear it it performs a task, in this case it runs a .sh.I want this script to always be running in the background since sometimes I will be able to cancel the shutdown, so if it ends after shutdown it wont be able to perform the task again later on.

Does anyone know any way to do this? I have tried creating a .service in multiple different ways but it doesn't work, or it works once and then doesn't.

This is my .sh, which always works if i just run it like ./check_pwr:

#!/bin/bashpython3 /home/workspace/on_boot/on_key.pytimestamp=$(date +"%Y-%m-%d %T")echo "$timestamp - System is shutting down" >> /home/workspace/on_boot/pwr.log

My .service is something like this:

[Unit]Description=My Shutdown ListenerDefaultDependencies=noBefore=shutdown.target[Service]Type=oneshotExecStart=/home/y3qucfm/workspace/on_boot/check_pwr.shRemainAfterExit=true[Install]WantedBy=shutdown.target

Viewing all articles
Browse latest Browse all 5962

Trending Articles