Make instructions for Debian+systemd to run a bash script right before shutting down…
- User
Make instructions for Debian+systemd to run a bash script right before shutting down each time the system is told to shut down. Be very concise.
- Assistant
To execute a bash script before shutting down on Debian using systemd, follow these concise instructions:
-
Create a bash script (e.g., "myscript.sh") that contains the desired commands or actions you want to perform before shutdown.
-
Move the script to the "/usr/local/bin" directory:
$ sudo mv myscript.sh /usr/local/bin/ -
Open a new systemd service unit file for editing:
$ sudo nano /etc/systemd/system/myscript.service -
Add the following content to the service unit file:
[Unit] Description=My Script Before Shutdown [Service] Type=oneshot RemainAfterExit=true ExecStart=/bin/true ExecStop=/usr/local/bin/myscript.sh [Install] WantedBy=multi-user.target -
Save the file and exit the text editor.
-
Enable the new service:
$ sudo systemctl enable myscript.service -
Reboot your system to activate the new service:
$ sudo systemctl reboot
From now on, your "myscript.sh" will be executed each time the system is commanded to shut down.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.