1

I want to run a script at shutdown and reboot after rsyslog has stopped.

What do I have to write in my systemd service file /lib/systemd/system/test.service. So far I have

[Unit]
Description=test
RequiresMountsFor=/

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/which/path/should/itbe

[Install]
WantedBy=reboot.target halt.target shutdown.target poweroff.target

How can I run this after rsyslog has stopped?

This seems to be the only way How to run a script at shutdown on Debian 9 or Raspbian 8 (Jessie).

1 Answer 1

1

You should add an Before=rsyslog.service in the [Unit] section

By the way, you should probably change the last line with WantedBy=multi-user.target

5
  • Do you mean After=rsyslog.service? And what's the logic behind WantedBy=multi-user.target? It is not so easy to find useful documentation on this. Feb 24, 2017 at 20:23
  • 1
    Regarding WantedBy=multi-user.target it means that it will be added to the dependency chain that leads to the multi-user.target (which is the default target started on a machine) And I was wrong, if you want your service to be stopped After rsyslog you need to set Before=rsyslog.service as the order is reversed between the boot and the shutdown.
    – Bigon
    Feb 24, 2017 at 20:34
  • Thanks. I also read about the reversed order but nothing official and got confused. But still I am surprised that I have to start a service for another target that what I want to run it for. Anyway I read it at other places too and will do it. But it seem strange. Feb 24, 2017 at 20:42
  • 2
    Here you are stopping the service, not starting it. You could also use ExecStart= but the configuration would even be more complex. Regarding the dependencies at shutdown (freedesktop.org/software/systemd/man/systemd.unit.html): "Note that when two units with an ordering dependency between them are shut down, the inverse of the start-up order is applied"
    – Bigon
    Feb 24, 2017 at 20:56
  • Ok, thanks. I tested it and it works. Here is the working example. Feb 26, 2017 at 15:05

You must log in to answer this question.

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