D
D
Dannerty2018-07-11 14:19:26
linux
Dannerty, 2018-07-11 14:19:26

What is the correct way to write Unit systemd?

Tell me how to write a unit correctly so that it runs the script on shutdown / reboot / sleep?
Now it's written like this

[Unit]
 Before=hibernate.target hybrid-sleep.target suspend.target shutdown.target poweroff.target
 After=network.target

[Service]
 Type=oneshot
 ExecStart=/bin/bash -c "/etc/init.d/zabbix-4shutdown.script stop"
 RemainAfterExit=true

[Install]
 WantedBy=graphical.target

But it doesn't work when turned off. If you do systemctl restart myservice, then the script is started and executed. I suspect that the error was in the Install block, I did it with multi-user.target, shutdown.target, but it didn’t work either.
UPD.
It turned out that not everything is working correctly.
Prescribed
[Unit]
 Before=shutdown.target poweroff.target
 Conflicts=reboot.target
 Requires=network.target

[Service]
 Type=oneshot
 ExecStop=/bin/bash -c "/etc/init.d/zabbix_4shutdown.script stop"
 RemainAfterExit=true

[Install]
 WantedBy=network.target

And the script is still launched on reboot, but it starts in such a way that it cannot send anything, because. either the network is already down, or openvpn is cut off before the script starts. I need to run different scripts on shutdown and on reboot, but for some reason Conflicts\Before are not processed correctly.
Is it possible to bind to WantedBy=openvpn or something like that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vsevolod, 2018-07-11
@Dannerty

Yes, I also encountered this.
I can’t remember exactly what happened now, but you can try:
Requires=graphical.target
then it will be cut down when graphical.target is cut down.
Or something more reasonable to cling to.
Let's say to network.target.
Without a network, then zabbix is ​​definitely not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question