How to run a script on system shutdown?

Hi!
I would like to run a script on the system shutdown.
I have read this article https://www.golinuxcloud.com/run-script-with-systemd-before-shutdown-linux/
but what I do doesn’t seem to work.

I have created a text file … /etc/systemd/system/run-before-shutdown.service
with this content …

[Unit]
Description=Run veracrypt task at shutdown
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/apps/veracrypt/unmount.sh
TimeoutStartSec=0

[Install]
WantedBy=shutdown.target

Then I have also created another text file … /apps/veracrypt/unmount.sh
with this content …

#!/bin/sh
veracrypt -d -f
sleep 3 

Then I have assigned permission for this last text file like this …

chmod u+x unmount.sh

Am I missing something?
The “veracrypt -d -f” sentence needs root permission.
Is that the problem?
If that’s the case, how do I solve it?

I’m sorry if my English is not good enough.
Thanks in advance.
Greetings!

Hello @josete :wink:

I bet you need the full path to veracrypt.

ExecStart=/usr/bin/veracrypt -d -f
TimeoutSec=3

:wink:

Thanks a lot for your answer.
I’ve made what you have suggested but I’m afraid there’s no change.
I wonder how I can check if this is really doing something.
I’ve tried “journalctl -b -1” but the answer to that is “no persistent journal was found
:thinking:

Instead try this for example:

journalctl --since="-15min"

or

journalctl --unit="run-before-shutdown.service"

that is not a standard directory/path
(maybe it is - with app-images and snaps and the like … I don’t know)

you also referred to it as:

… /apps/veracrypt/unmount.sh

hmm - that’s not a proper path

But, I guess, it needs to be in the $PATH that systemd knows of - to find and execute it.

/apps/ is a partition I use for things related to applications.
I place there … appimages, templates, virtual machines, timeshift images …
If I use “/apps/veracrypt/unmount.sh” I guess I don’t need to have that in the $PATH

I guess I really don’t know whether that is indeed so. :wink:
I’d rather put scripts in a standard location - but this is just my guess, as I said.

It depends where veracrypt is located:

which veracrypt

Hi, megavolt! Thanks again for your answer.
I’m afraid I can’t see anything from previous sessions with journalctl.
There’s nothing recorded.
It always begins with the current one.
So I can’t see any message related to the previous shutdown.

which veracrypt

returns …

/usr/bin/veracrypt

and the file unmount.sh now contains …

#!/bin/sh
ExecStart=/usr/bin/veracrypt -d -f
sleep 3

so I guess it’s okay.

What I need now is information about the result of both files (sh and service) in the shutdown process.
But I don’t know how to get it.
If everything where working properly I wouldn’t see error messages about veracrypt volumes not unmounted.

I’ve read that I should execute this command …

sudo touch /var/log/journal

I have created the journal directory.
Now I can execute journalctl -b -1 > list.txt
I’ve found this …

app-veracrypt-cd3c7135cc094b4e9a4a60911dbbd40b.scope: Failed to kill control group /user.slice/user-1000.slice/user@1000.service/app.slice/app-veracrypt-cd3c7135cc094b4e9a4a60911dbbd40b.scope, ignoring: Operation not permitted
…: app-veracrypt-cd3c7135cc094b4e9a4a60911dbbd40b.scope: Killing process 2387 (veracrypt) with signal SIGKILL.
…: app-veracrypt-cd3c7135cc094b4e9a4a60911dbbd40b.scope: Killing process 2389 (veracrypt) with signal SIGKILL.
…: app-veracrypt-cd3c7135cc094b4e9a4a60911dbbd40b.scope: Killing process 2437 (mount.ntfs) with signal SIGKILL.
…: app-veracrypt-cd3c7135cc094b4e9a4a60911dbbd40b.scope: Failed to kill control group /user.slice/user-1000.slice/user@1000.service/app.slice/app-veracrypt-cd3c7135cc094b4e9a4a60911dbbd40b.scope, ignoring: Operation not permitted
…: app-veracrypt-cd3c7135cc094b4e9a4a60911dbbd40b.scope: Succeeded.
…: Stopped Vera Crypt.

I’m afraid I’m completely lost!

Sorry, but this will not work. :slight_smile:

This file: /etc/systemd/system/run-before-shutdown.service must contain:

[Unit]
Description=Run veracrypt task at shutdown
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/veracrypt -d -f
TimeoutSec=3

[Install]
WantedBy=shutdown.target
1 Like

Okay! I have mixed several ideas, concepts, instructions … :slight_smile:
So it’s obvious I don’t need a script and a service. The service is enough.

I tried again with that change and it didn’t work.
But I knew it was a good step in the right direction.
I kept looking and found what was missing.

sudo systemctl daemon-reload
sudo systemctl enable run-before-shutdown

The answer to the last command was …

Created symlink /etc/systemd/system/shutdown.target.wants/run-before-shutdown.service → /etc/systemd/system/run-before-shutdown.service

And now IT WORKS perfectly.
I don’t see any error messages if I don’t unmount the veracrypt volumes before shutting down.

THANK YOU EVERYBODY!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.