I know that, with systemd, any scripts put in /lib/systemd/system-shutdown/ will launch at the very end of a shutdown sequence.

Is there a similar type of thing for scripts that I wish to launch immediately? Without a service file?

  • call shutdown at the end of your script
    – Panther
    Apr 12, 2018 at 1:43
  • The process goes; I call shutdown, and want some scripts to run, then the systemd service wind-down, then the scripts in system-shutdown Apr 12, 2018 at 3:42
  • Put all those commands, in the order you want them, which I am not sure I understand what you want when in the entire process, systemd service wind-down , your other commands, then, finally, at the end, the final shutdown sequence.
    – Panther
    Apr 12, 2018 at 5:36
  • What? This isn't a single program. I'm shutting down the whole system. I'm literally just typing shutdown or reboot into the terminal window. Systemd takes over at that point, and will shutdown services in reverse order that they came up at boot, and finally will execute all scripts in the system-shutdown dir at the very end just before the system is powered off. I need to tell systemd to run something before everything else on shutdown. Apr 12, 2018 at 7:38

1 Answer 1


As you mentioned in a comment, systemd shuts down services in the reverse order that they are started.

You can create a new target that forces your command to be started last and shutdown first. Then set your system to boot to this new "target" state instead of the "multi-user.target" or "graphical.target".

In your new service, set ExecStop= to be the thing you want to run first on shutdown.

  • On my computer, the whole filesystem is loaded and accessed by RAM. This means any changes you make while running get blown away. This means I can't change this new target to the default using systemctl commands after boot. In this target's file, do you know if the Alias=default.target would change this target to the default target on boot? Apr 13, 2018 at 16:38
  • Then you may need to change the media you boot from. Apr 13, 2018 at 18:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .