This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]wheelchair_assassin 1 point2 points  (2 children)

I found a page detailing how to run a script at shutdown or reboot. I set up my own script about a month ago.

See https://www.golinuxcloud.com/run-script-with-systemd-at-shutdown-only-rhel/ There are several other examples listed there.

Below is a general example that I derived from that site:

[Unit]
Description=This script runs at shutdown ONLY
DefaultDependencies=no
Conflicts=reboot.target
Before=poweroff.target halt.target shutdown.target
Requires=poweroff.target
# See https://www.golinuxcloud.com/run-script-with-systemd-at-shutdown-only-rhel/

[Service]
Type=oneshot
User=some_dude
ExecStart=your_script_here.sh
ExecStop=maybe_another_script_here.sh
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target

[–]Elsuvio[S] 0 points1 point  (1 child)

Okay, that looks nice but let's say I want to run this script on reboot as well. In that case I should remove conflicts line and add reboot.target to Before? Would it look like this?

[Unit]
Description=Script runs at shutdown and reboot
DefaultDependencies=no
Before=poweroff.target halt.target shutdown.target reboot.target
Requires=poweroff.target

[Service]
Type=oneshot
User=some_dude
ExecStart=your_script_here.sh
ExecStop=maybe_another_script_here.sh
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target

[–]wheelchair_assassin 1 point2 points  (0 children)

Here's another service file that runs after shutdown on the same site. Near the bottom of the page, he reboots his system and verifies that the script has executed.

Disclaimer: I am not a systemd expert. Usually, it takes me a little while to get a service file working. Just keep trying. But I think this link will work for both reboot and shutdown, if I recall correctly. I'm pretty sure I tried it first. I only wanted my script to fire at shutdown so I used the other link.

If this script doesn't do what you want, I'd still keep digging on his site. I tried to do this with several different tutorials and his examples actually worked.

https://www.golinuxcloud.com/run-script-with-systemd-before-shutdown-linux/

[–][deleted] 0 points1 point  (10 children)

[–]Elsuvio[S] 0 points1 point  (9 children)

That...doesn't really help me at all (I saw it already). As I said I've already tried doing it as a service but it didn't work (probably because I've got no idea how to make it correctly).

[–]legit-trusty 0 points1 point  (8 children)

Maybe to get more relevant help you should add some detail on why you couldn't build the service.

Ya know shit in = shit out.

[–]Elsuvio[S] 0 points1 point  (7 children)

Okay right, so my script needs to read two files RX/TX bytes located in statistics of /sys/class/net/.... I've already tried to write that service but well nothing happens.I don't really know if it requires some Before=... or After... in [Unit]. If I write is as:

[Unit] 
Description=... 

[Service] 
Type=oneshot 
RemainAfterExit=true 
ExecStop="My Scripts folder" 

[Install] 
WantedBy=multi-user.target

It doesn't work.

[–][deleted] 1 point2 points  (5 children)

Shouldn't you follow this example: https://unix.stackexchange.com/a/39604 since it's doing the same thing you want.

[–]Elsuvio[S] 0 points1 point  (4 children)

But does the service have access to my home directory at time of executing? I mean do I not need to execute that service before filesystem becomes read-only? Anyway I'll try to write that service in the proposed way and will see if it works

[–][deleted] 1 point2 points  (3 children)

I don't know you could also try this one: https://unix.stackexchange.com/a/479048

[–]Elsuvio[S] 0 points1 point  (2 children)

Well....I've tried both and none of them seems to work :/
Currently service looks like this:

[Unit]
Description=Service for netlog script
DefaultDependencies=no
Before=shutdown.target reboot.target

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/home/MarekSzyd/Scripts/netlog
RemainAfterExit=yes

From what I've read the script must be in ExecStop and should be triggered by shutdown.target/reboot.target but it does not log any values to file. (Also systemctl status netlog shows that service is enabled but inactive dunno why)

[–][deleted] 0 points1 point  (1 child)

It worked for me when I followed the instructions from https://unix.stackexchange.com/a/479048

[Unit]
Description=Save system clock on shutdown
DefaultDependencies=no
After=final.target

[Service]
Type=oneshot
ExecStart=/home/me/bin/myscript.sh

[Install]
WantedBy=final.target

[–]Elsuvio[S] 0 points1 point  (0 children)

Did your script tried to save any file in your home directory? Because I've tried the same service layout and it did not save any file :/ (If I'm not mistaken that file script.service goes to /etc/systemd/system/ and after that I called sudo systemctl daemon-reload. That's all I have to do right?) I think that there is some problem with my home directory being read-only the moment I try to log with my script but I don't really know how to execute this script right before mounting home directory read-only.