[systemd-devel] At wits end... need to execute a script prior to anything getting killed/changed on reboot/shutdown

Christopher Cox ccox at endlessnow.com
Thu Jan 17 04:49:40 UTC 2019


On 01/16/2019 10:44 PM, Christopher Cox wrote:
> On 01/16/2019 12:51 PM, Filipe Brandenburger wrote:
>> If you want to run it early in the shutdown process, then keep 
>> DefaultDependencies=yes, in which case it will run before the base 
>> dependencies start to get stopped.
>>
>> If you need some other resources to be up, for instance network, then add 
>> After=network.target, etc.
>>
>> Remember that when shutting down, the dependencies are stopped in the 
>> opposite order as they're started up, so if you need your script to run 
>> *before* something else is stopped, then you need an After= dependency.
>>
> But only for services under systemd.  Everything else gets sprayed a TERM 
> signal.  Which is frustrating.  So... having it execute first doesn't 
> guarantee (and we're talking less than a second or so) that it will have time 
> to do what it needs to do before the kill spray happens, and I need all 
> processes to be around until my script finishes.
>
> Old sysvinit didn't have this limitation.

Adding some extra systemd clarification.  Saying do this After or Before other 
service doesn't mean the start/stop completes before moving on.  It may execute 
asynchronously before/after, but processes aren't done synchronously.

>
>> You shouldn't add any ordering regarding reboot.target and shutdown.target. 
>> Just enable your service (so that it looks up during normal system usage), 
>> when the system goes down it will be stopped, and then depending on its 
>> After= dependencies it will block those other services from being stopped 
>> until you're done.
>>
>> In recent systemd versions (I think starting from v238?) you can omit the 
>> ExecStart=/bin/true line, an unit without that line starts to be valid in one 
>> of those versions... Though keeping it around is fine and will work with 
>> older versions too.
> Has anyone tried to do something (I'm on CentOS latest) with maybe an extra 
> target (post default.target)... possible.  I need something that "works".
>>
>> I hope this helps!
>>
>> Cheers,
>> Filipe
>>
>>
>> On Wed, Jan 16, 2019 at 10:47 AM Christopher Cox <ccox at endlessnow.com 
>> <mailto:ccox at endlessnow.com>> wrote:
>>
>>     I need to be able to execute a script before anything gets shutdown.  That
>>     is,
>>     when somebody does a "reboot", "shutdown" or "poweroff", I need this
>>     script to
>>     run first, and for it to finish before everything gets whacked.
>>
>>     I know the following isn't "right"... I've tried so many different things.
>>     Google hasn't helped only giving me many "right" solutions that didn't
>>     work. In
>>     this current edition, basically I get a partial capture of processes that 
>> are
>>     running (that is some were killed directly or indirectly).. I need them
>>     all.  My
>>     script needs to see the state of operation before 
>> reboot/shutdown/poweroff do
>>     anything else.  My "save" saves some information about running processes
>>     (some
>>     not necessarily under systemd control).
>>
>>     [Unit]
>>     Description=my-service save status
>>     DefaultDependencies=no
>>     Before=reboot.target shutdown.target
>>     Conflicts=reboot.target shutdown.target
>>
>>     [Service]
>>     Type=oneshot
>>     RemainAfterExit=yes
>>     ExecStart=/bin/true
>>     ExecStop=/usr/local/bin/my-service.sh save
>>     StandardOutput=journal
>>
>>     [Install]
>>     WantedBy=multi-user.target
>>
>>     _______________________________________________
>>     systemd-devel mailing list
>>     systemd-devel at lists.freedesktop.org
>>     <mailto:systemd-devel at lists.freedesktop.org>
>>     https://lists.freedesktop.org/mailman/listinfo/systemd-devel
>>
>



More information about the systemd-devel mailing list