What do I need to put in the [install]
section, so that systemd runs /home/me/so.pl
right before shutdown and also before /proc/self/net/dev
gets destroyed?
[Unit]
Description=Log Traffic
[Service]
ExecStart=/home/me/so.pl
[Install]
?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityThe suggested solution is to run the service unit as a normal service - have a look at the [Install]
section. So everything has to be thought reverse, dependencies too. Because the shutdown order is the reverse startup order. That's why the script has to be placed in ExecStop=
.
The following solution is working for me:
[Unit]
Description=...
[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=<your script/program>
[Install]
WantedBy=multi-user.target
RemainAfterExit=true
is needed when you don't have an ExecStart
action.
After creating the file, make sure to systemctl daemon-reload
and systemctl enable yourservice --now
.
I just got it from systemd IRC, credits are going to mezcalero.
RemainAfterExit=true
is required when there is no ExecStart
is because systemd
will not attempt to run ExecStop
if it thinks that the service is not running. RemainAfterExit=true
causes systemd
to believe that the service is running, thereby causing it to run ExecStop
at shutdown.
Jun 15, 2017 at 0:30
ExecStop
begin execution but get killed before the process is able to complete? Something else?
Sep 26, 2019 at 19:46
ExecStart=/bin/true
for this to work, adding RemainAfterExit=true
(without ExecStart
) was not enough for me to get this to work on Fedora.
May 4, 2020 at 17:26
To run a service right before starting any of reboot/shutdown/halt/kexec services (i.e. in the last moment before root filesystem becomes remounted read-only) use this service config:
[Unit]
Description=Save system clock on shutdown
DefaultDependencies=no
After=final.target
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/fake-hwclock.sh save
[Install]
WantedBy=final.target
Enable it with:
systemctl enable my_service.service
To run a script right before actual reboot/shutdown/halt/kexec (when you cannot write to the root filesystem, because it was remounted read-only) add this script executable to the /usr/lib/systemd/system-shutdown
directory.
Immediately before executing the actual system halt/poweroff/reboot/kexec systemd-shutdown will run all executables in /usr/lib/systemd/system-shutdown/ and pass one arguments to them: either "halt", "poweroff", "reboot" or "kexec", depending on the chosen action. All executables in this directory are executed in parallel, and execution of the action is not continued before all executables finished.
Also see:
https://www.freedesktop.org/software/systemd/man/bootup.html
https://www.freedesktop.org/software/systemd/man/systemd-halt.service.html
final.target
. Ideally I would like that it is the first thing being executed after the user do $ sudo reboot
.
May 25, 2019 at 22:29
/etc/adjtime
then. This behavior is also what I expected after looking at the linked chart - because the umount.target
readonly-remounts the root filesystem and final.target
depends on that target.
Jun 21, 2020 at 10:05
/lib/systemd/system-shutdown/
.
Aug 7 at 15:59
I am not totally sure but i don't think you need the install part though i added it explicitly. I also didn't test it but i think it should help you get started:
[Unit]
Description=Log Traffic
Requires=network.target
After=network.target
Before=shutdown.target
DefaultDependencies=no
[Service]
ExecStart=/home/me/so.pl
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=shutdown.target
As far as I can see this does what I need (but I don't know exactly why).
[Unit]
Description=Log Traffic
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
[Service]
ExecStart=/usr/local/bin/perl /home/me/log_traffic.pl --stop
Type=oneshot
WantedBy=shutdown.target reboot.target halt.target
to the [Unit]
section. Before=
& After=
don't change dependencies.