Is it possible to run a script before the shutdown? That means at a point where the system is still fully functional.
I tried to put my shutdown script in /lib/systemd/system-shutdown/test-script
. It will be executed but from my understanding it is only executed immediately before the shutdown takes place. For instance, the disk is already mounted in read-only mode. This can be fixed, but wrapping the script with:
#!/bin/bash
mount -oremount,rw /
... do something ...
mount -oremount,ro /
What I require and cannot get to work, though, is internet connection. When the script is executed, the internet connection seems to be no longer available.
Is it possible to run a script right at the begin of the shutdown protocol?
I'm testing with Ubuntu 16.04.
I found the answer on https://unix.stackexchange.com/q/39226/29509.
To sum it up, this solution works for me (on Ubuntu 16.04):
Create a file
/etc/systemd/system/my-cleanup.service
:(Assuming the shutdown script is available under
/path/to/my-cleanup-script.sh
and is executable.)Finally, enable the systemd service via:
With respect to Answer 1. I trust the solution will work "during" shutdown but as the OP asked
it will run right at the begin of the shutdown
, I am not sure.This link is more explicit about the moment a script will be executed during shutdown: http://ccm.net/faq/3348-execute-a-script-at-startup-and-shutdown-on-ubuntu
Please note I did not test it in my system, I'm just relaying my findings.