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

2019-01-26 Thread Andrei Borzenkov
17.01.2019 21:03, Christopher Cox пишет:
> On 1/17/19 11:59 AM, Lennart Poettering wrote:
>> On Do, 17.01.19 11:17, Christopher Cox (c...@endlessnow.com) wrote:
>>
> [Install]
> WantedBy=multi-user.target
>
> In my case, my script rolls through the currently running
> processes, looking
> for certain ones, determines listening port (ss) and gets the time the
> process was started (stat) and outputs info to a file.
>
> What I'm seeing is a file at shutdown that does not contain all the
> processes.
 Hmm, what kind of processes are you missing? user session stuff? How
 do you shut down? Note that display managers are likely to terminate
 the user sessions first, and only initiate system shutdown then...
>>> These are nohup'd background processes not tied to any tty.
>> Well, how is that stuff started? Note that if systemd --user or
>> --system manages your process then it will keep track of it through
>> cgroups, and "nohup" is not a concept for evading that.
> 
> Nohup'd processes started by an ssh as a user.  Process "parent" pid 1.
> 

And they belong to which control group (i.e. service)?

What dependencies this service has?
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Christopher Cox

On 1/17/19 2:42 PM, Lennart Poettering wrote:

On Do, 17.01.19 14:35, Christopher Cox (c...@endlessnow.com) wrote:


On 1/17/19 2:25 PM, Lennart Poettering wrote:

On Do, 17.01.19 12:38, Christopher Cox (c...@endlessnow.com) wrote:


it defaults to YES and the whole discussions as that changed where about
nohup'd processes long ago


Changing it to "no"... I'll let you know if this fixes things or not.



Actually, as it turns out the nohup'd processes are all owned by root, so
changing to "no" didn't fix, but it's my understanding that if the setting
isn't set root is always excluded anyhow.


The sessions of root are excluded, which is semantically slightly
different from processes of root.


Out of the 18 processes that are
running, my script only sees 6 of them.  Again, it's just doing a "ps -ef"
to a file.  All 18 processes exist prior to shutdown and the script shows
that if I run  manually.


Which systemd version is this? Note that on old systemd versions
systemd-user-sessions.service would go on its own killing spree early
on. Maybe you have such an old version?


Quite possible.  This is CentOS 7.6 using what it calls "systemd-219-62"


So, if you order your service After=systemd-user-sessions.service,
does that change things?


I changed it to use After=systemd-user-sessions.service

Did not seem to change the behavior.  Only getting subset in the file 
(this time only 2 of 18 processes).



But, systemctl list-dependencies --after systemd-user-sessions.service, 
doesn't show my service.  Should it?

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Lennart Poettering
On Do, 17.01.19 14:35, Christopher Cox (c...@endlessnow.com) wrote:

> On 1/17/19 2:25 PM, Lennart Poettering wrote:
> > On Do, 17.01.19 12:38, Christopher Cox (c...@endlessnow.com) wrote:
> >
> > > > > it defaults to YES and the whole discussions as that changed where 
> > > > > about
> > > > > nohup'd processes long ago
> > > >
> > > > Changing it to "no"... I'll let you know if this fixes things or not.
> > > >
> > >
> > > Actually, as it turns out the nohup'd processes are all owned by root, so
> > > changing to "no" didn't fix, but it's my understanding that if the setting
> > > isn't set root is always excluded anyhow.
> >
> > The sessions of root are excluded, which is semantically slightly
> > different from processes of root.
> >
> > > Out of the 18 processes that are
> > > running, my script only sees 6 of them.  Again, it's just doing a "ps -ef"
> > > to a file.  All 18 processes exist prior to shutdown and the script shows
> > > that if I run  manually.
> >
> > Which systemd version is this? Note that on old systemd versions
> > systemd-user-sessions.service would go on its own killing spree early
> > on. Maybe you have such an old version?
>
> Quite possible.  This is CentOS 7.6 using what it calls "systemd-219-62"

So, if you order your service After=systemd-user-sessions.service,
does that change things?

Lennart

--
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Christopher Cox

On 1/17/19 2:25 PM, Lennart Poettering wrote:

On Do, 17.01.19 12:38, Christopher Cox (c...@endlessnow.com) wrote:


it defaults to YES and the whole discussions as that changed where about
nohup'd processes long ago


Changing it to "no"... I'll let you know if this fixes things or not.



Actually, as it turns out the nohup'd processes are all owned by root, so
changing to "no" didn't fix, but it's my understanding that if the setting
isn't set root is always excluded anyhow.


The sessions of root are excluded, which is semantically slightly
different from processes of root.


Out of the 18 processes that are
running, my script only sees 6 of them.  Again, it's just doing a "ps -ef"
to a file.  All 18 processes exist prior to shutdown and the script shows
that if I run  manually.


Which systemd version is this? Note that on old systemd versions
systemd-user-sessions.service would go on its own killing spree early
on. Maybe you have such an old version?


Quite possible.  This is CentOS 7.6 using what it calls "systemd-219-62"

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Lennart Poettering
On Do, 17.01.19 12:38, Christopher Cox (c...@endlessnow.com) wrote:

> > > it defaults to YES and the whole discussions as that changed where about
> > > nohup'd processes long ago
> >
> > Changing it to "no"... I'll let you know if this fixes things or not.
> >
>
> Actually, as it turns out the nohup'd processes are all owned by root, so
> changing to "no" didn't fix, but it's my understanding that if the setting
> isn't set root is always excluded anyhow.

The sessions of root are excluded, which is semantically slightly
different from processes of root.

> Out of the 18 processes that are
> running, my script only sees 6 of them.  Again, it's just doing a "ps -ef"
> to a file.  All 18 processes exist prior to shutdown and the script shows
> that if I run  manually.

Which systemd version is this? Note that on old systemd versions
systemd-user-sessions.service would go on its own killing spree early
on. Maybe you have such an old version?

Lennart

--
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Christopher Cox

On 1/17/19 12:09 PM, Christopher Cox wrote:

On 1/17/19 11:54 AM, Reindl Harald wrote:


Am 17.01.19 um 18:51 schrieb Christopher Cox:

On 1/17/19 11:21 AM, Reindl Harald wrote:

Am 17.01.19 um 18:17 schrieb Christopher Cox:

On 1/17/19 11:01 AM, Lennart Poettering wrote:

Hmm, what kind of processes are you missing? user session stuff? How
do you shut down? Note that display managers are likely to terminate
the user sessions first, and only initiate system shutdown then...

These are nohup'd background processes not tied to any tty.

give that a try!

[root@srv-rhsoft:~]$ cat /etc/systemd//logind.conf.d/logind.conf
[Login]
KillUserProcesses=no

It's default, that is, already set to "no" (shouldn't matter anyway,
again the processes are nohup'd)

it is NOT default

it defaults to YES and the whole discussions as that changed where about
nohup'd processes long ago


Changing it to "no"... I'll let you know if this fixes things or not.



Actually, as it turns out the nohup'd processes are all owned by root, 
so changing to "no" didn't fix, but it's my understanding that if the 
setting isn't set root is always excluded anyhow.  Out of the 18 
processes that are running, my script only sees 6 of them.  Again, it's 
just doing a "ps -ef" to a file.  All 18 processes exist prior to 
shutdown and the script shows that if I run  manually.

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Christopher Cox

On 1/17/19 11:54 AM, Reindl Harald wrote:


Am 17.01.19 um 18:51 schrieb Christopher Cox:

On 1/17/19 11:21 AM, Reindl Harald wrote:

Am 17.01.19 um 18:17 schrieb Christopher Cox:

On 1/17/19 11:01 AM, Lennart Poettering wrote:

Hmm, what kind of processes are you missing? user session stuff? How
do you shut down? Note that display managers are likely to terminate
the user sessions first, and only initiate system shutdown then...

These are nohup'd background processes not tied to any tty.

give that a try!

[root@srv-rhsoft:~]$ cat /etc/systemd//logind.conf.d/logind.conf
[Login]
KillUserProcesses=no

It's default, that is, already set to "no" (shouldn't matter anyway,
again the processes are nohup'd)

it is NOT default

it defaults to YES and the whole discussions as that changed where about
nohup'd processes long ago


Changing it to "no"... I'll let you know if this fixes things or not.

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Mike Gilbert
On Thu, Jan 17, 2019 at 12:54 PM Reindl Harald  wrote:
>
>
>
> Am 17.01.19 um 18:51 schrieb Christopher Cox:
> > On 1/17/19 11:21 AM, Reindl Harald wrote:
> >>
> >> Am 17.01.19 um 18:17 schrieb Christopher Cox:
> >>> On 1/17/19 11:01 AM, Lennart Poettering wrote:
>  Hmm, what kind of processes are you missing? user session stuff? How
>  do you shut down? Note that display managers are likely to terminate
>  the user sessions first, and only initiate system shutdown then...
> >>> These are nohup'd background processes not tied to any tty.
> >> give that a try!
> >>
> >> [root@srv-rhsoft:~]$ cat /etc/systemd//logind.conf.d/logind.conf
> >> [Login]
> >> KillUserProcesses=no
> >
> > It's default, that is, already set to "no" (shouldn't matter anyway,
> > again the processes are nohup'd)
>
> it is NOT default
>
> it defaults to YES and the whole discussions as that changed where about
> nohup'd processes long ago

Unless he is compiling systemd himself, the default is determined by
the distro he is using. Several distros default it to "no" via the
default-kill-user-processes meson option.
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Christopher Cox

On 1/17/19 11:59 AM, Lennart Poettering wrote:

On Do, 17.01.19 11:17, Christopher Cox (c...@endlessnow.com) wrote:


[Install]
WantedBy=multi-user.target

In my case, my script rolls through the currently running processes, looking
for certain ones, determines listening port (ss) and gets the time the
process was started (stat) and outputs info to a file.

What I'm seeing is a file at shutdown that does not contain all the
processes.

Hmm, what kind of processes are you missing? user session stuff? How
do you shut down? Note that display managers are likely to terminate
the user sessions first, and only initiate system shutdown then...

These are nohup'd background processes not tied to any tty.

Well, how is that stuff started? Note that if systemd --user or
--system manages your process then it will keep track of it through
cgroups, and "nohup" is not a concept for evading that.


Nohup'd processes started by an ssh as a user.  Process "parent" pid 1.


___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Lennart Poettering
On Do, 17.01.19 11:17, Christopher Cox (c...@endlessnow.com) wrote:

> > > [Install]
> > > WantedBy=multi-user.target
> > >
> > > In my case, my script rolls through the currently running processes, 
> > > looking
> > > for certain ones, determines listening port (ss) and gets the time the
> > > process was started (stat) and outputs info to a file.
> > >
> > > What I'm seeing is a file at shutdown that does not contain all the
> > > processes.
> > Hmm, what kind of processes are you missing? user session stuff? How
> > do you shut down? Note that display managers are likely to terminate
> > the user sessions first, and only initiate system shutdown then...
>
> These are nohup'd background processes not tied to any tty.

Well, how is that stuff started? Note that if systemd --user or
--system manages your process then it will keep track of it through
cgroups, and "nohup" is not a concept for evading that.

Lennart

--
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Reindl Harald


Am 17.01.19 um 18:51 schrieb Christopher Cox:
> On 1/17/19 11:21 AM, Reindl Harald wrote:
>>
>> Am 17.01.19 um 18:17 schrieb Christopher Cox:
>>> On 1/17/19 11:01 AM, Lennart Poettering wrote:
 Hmm, what kind of processes are you missing? user session stuff? How
 do you shut down? Note that display managers are likely to terminate
 the user sessions first, and only initiate system shutdown then...
>>> These are nohup'd background processes not tied to any tty.
>> give that a try!
>>
>> [root@srv-rhsoft:~]$ cat /etc/systemd//logind.conf.d/logind.conf
>> [Login]
>> KillUserProcesses=no
> 
> It's default, that is, already set to "no" (shouldn't matter anyway,
> again the processes are nohup'd)

it is NOT default

it defaults to YES and the whole discussions as that changed where about
nohup'd processes long ago

https://news.ycombinator.com/item?id=14734854

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Christopher Cox

On 1/17/19 11:21 AM, Reindl Harald wrote:


Am 17.01.19 um 18:17 schrieb Christopher Cox:

On 1/17/19 11:01 AM, Lennart Poettering wrote:

Hmm, what kind of processes are you missing? user session stuff? How
do you shut down? Note that display managers are likely to terminate
the user sessions first, and only initiate system shutdown then...

These are nohup'd background processes not tied to any tty.

give that a try!

[root@srv-rhsoft:~]$ cat /etc/systemd//logind.conf.d/logind.conf
[Login]
KillUserProcesses=no


It's default, that is, already set to "no" (shouldn't matter anyway, 
again the processes are nohup'd)



___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Reindl Harald


Am 17.01.19 um 18:17 schrieb Christopher Cox:
> On 1/17/19 11:01 AM, Lennart Poettering wrote:
>> Hmm, what kind of processes are you missing? user session stuff? How
>> do you shut down? Note that display managers are likely to terminate
>> the user sessions first, and only initiate system shutdown then...
> 
> These are nohup'd background processes not tied to any tty.

give that a try!

[root@srv-rhsoft:~]$ cat /etc/systemd//logind.conf.d/logind.conf
[Login]
KillUserProcesses=no
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Christopher Cox

On 1/17/19 11:01 AM, Lennart Poettering wrote:

On Do, 17.01.19 10:18, Christopher Cox (c...@endlessnow.com) wrote:


On 1/17/19 5:50 AM, Lennart Poettering wrote:

With that you can now put together a unit that is terminated
relatively early on during shutdown: just make it
"After=multi-user.target graphical.target default.target", so that it
gets activated at boot very late, and thus deactivated at shutdown
very early.

Thanks, I think I had this on one of my many attempts.  But changed to suit.

[Unit]
Description=my-service-save save run state
After=multi-user.target graphical.target default.target

[Service]
Type=oneshot
ExecStop=/usr/local/bin/my-services.sh save
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

In my case, my script rolls through the currently running processes, looking
for certain ones, determines listening port (ss) and gets the time the
process was started (stat) and outputs info to a file.

What I'm seeing is a file at shutdown that does not contain all the
processes.

Hmm, what kind of processes are you missing? user session stuff? How
do you shut down? Note that display managers are likely to terminate
the user sessions first, and only initiate system shutdown then...


These are nohup'd background processes not tied to any tty.


___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Lennart Poettering
On Do, 17.01.19 10:18, Christopher Cox (c...@endlessnow.com) wrote:

> On 1/17/19 5:50 AM, Lennart Poettering wrote:
> > With that you can now put together a unit that is terminated
> > relatively early on during shutdown: just make it
> > "After=multi-user.target graphical.target default.target", so that it
> > gets activated at boot very late, and thus deactivated at shutdown
> > very early.
>
> Thanks, I think I had this on one of my many attempts.  But changed to suit.
>
> [Unit]
> Description=my-service-save save run state
> After=multi-user.target graphical.target default.target
>
> [Service]
> Type=oneshot
> ExecStop=/usr/local/bin/my-services.sh save
> RemainAfterExit=yes
>
> [Install]
> WantedBy=multi-user.target
>
> In my case, my script rolls through the currently running processes, looking
> for certain ones, determines listening port (ss) and gets the time the
> process was started (stat) and outputs info to a file.
>
> What I'm seeing is a file at shutdown that does not contain all the
> processes.

Hmm, what kind of processes are you missing? user session stuff? How
do you shut down? Note that display managers are likely to terminate
the user sessions first, and only initiate system shutdown then...

Lennart

--
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Christopher Cox

On 1/17/19 5:50 AM, Lennart Poettering wrote:

With that you can now put together a unit that is terminated
relatively early on during shutdown: just make it
"After=multi-user.target graphical.target default.target", so that it
gets activated at boot very late, and thus deactivated at shutdown
very early.


Thanks, I think I had this on one of my many attempts.  But changed to suit.

[Unit]
Description=my-service-save save run state
After=multi-user.target graphical.target default.target

[Service]
Type=oneshot
ExecStop=/usr/local/bin/my-services.sh save
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

In my case, my script rolls through the currently running processes, 
looking for certain ones, determines listening port (ss) and gets the 
time the process was started (stat) and outputs info to a file.


What I'm seeing is a file at shutdown that does not contain all the 
processes.  Running the script while in the normal run state works just 
fine.  But on shutdown, either the processes are gone or somehow my 
script is being terminated or something is preventing me from writing 
the whole file.


So, I'm still stuck on this one.


___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Reindl Harald


Am 17.01.19 um 17:00 schrieb Christopher Cox:
> On 1/16/19 11:10 PM, Reindl Harald wrote:
>> that all is not really new and was not better with sysvinit, it only was
>> slow enough, full of sleep/usleep hacks and so most of the time by luck
>> worked but with no guarantess anyways
> 
> What I said it that synchronous execution of a script was possible prior
> to "the kills" in sysvinit.
> 
> And that made it "better" in that regard.
> 
> And still, I have no means of doing this in systemd.  Anyone?

see the other responses and just use After/Before proper in all units
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Christopher Cox

On 1/16/19 11:10 PM, Reindl Harald wrote:

that all is not really new and was not better with sysvinit, it only was
slow enough, full of sleep/usleep hacks and so most of the time by luck
worked but with no guarantess anyways


What I said it that synchronous execution of a script was possible prior 
to "the kills" in sysvinit.


And that made it "better" in that regard.

And still, I have no means of doing this in systemd.  Anyone?

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Lennart Poettering
On Mi, 16.01.19 22:44, Christopher Cox (c...@endlessnow.com) 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.

In systemd the emphasis is on defining correct deps. If you service
needs some other service to shutdown properly, then it should order
itself After= that other service, which makes sure your service is
started after that other service started up, and stops before
that other service shuts down.

In general: if you are asking for absolutes such as "stop this first"
or "start this last", then most likely the better approach would be to
track down the actual deps you need, and use those instead...

> Old sysvinit didn't have this limitation.

Well, that's not precisely true. On LSB-style sysvinit you#d have to
define deps in the sysvinit script header for ordering to work
correctly. And on sysvinit predating that you'd have to use the right
priority values compare to the services you wanted to be ordered
after. This isn't too different from systemd really, except that we
generally parallelize and sysvinit was only parallelized on some
distros.

Lennart

--
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-17 Thread Lennart Poettering
On Mi, 16.01.19 12:40, Christopher Cox (c...@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.

Well, systemd doesn't know a concept of "before all" and "after all",
as a unit marked that way would have to be the only one, and what do
you do if multiple units are marked that way? Hence, systemd generally
allows you to order units after or before specific other units, but
there are no absolutes like "after all" or "before all".

If you want to define a unit that runs code during shutdown the best
approach is to make it a unit that gets started at boot, and then only
uses ExecStop= (but not necessarily ExecStart=).

Note that the shutdown order of units is generally the inverse of the
startup order. Hence if you want unit A to stop before unit B, then
you have to declare in A "After=B", so that A gets started *after*
unit B, and thus stopped before B, as you need it here.

Usually a system boots into multi-user.target or graphical.target (or
generically default.target), and these are the last (or close to last)
units that are reached. Due to the rule of "stop order is inverted
start order" this also means these targets are the first ones to be
stopped again when the system goes down.

With that you can now put together a unit that is terminated
relatively early on during shutdown: just make it
"After=multi-user.target graphical.target default.target", so that it
gets activated at boot very late, and thus deactivated at shutdown
very early.

> [Unit]
> Description=my-service save status
> DefaultDependencies=no

No need to turn off default dependencies.

> Before=reboot.target shutdown.target
> Conflicts=reboot.target shutdown.target

These two lines are unnecessary.

> [Service]
> Type=oneshot
> RemainAfterExit=yes
> ExecStart=/bin/true

This line is unnecessary on any remotely recent systemd version.

> ExecStop=/usr/local/bin/my-service.sh save
> StandardOutput=journal

This line is redundant, as it is the implied default.

> [Install]
> WantedBy=multi-user.target

Lennart

--
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-16 Thread Reindl Harald


Am 17.01.19 um 06:04 schrieb Jonathon Kowalski:
> On Thu, Jan 17, 2019 at 4:49 AM Christopher Cox  wrote:
>> 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.
>>
> 
> This isn't right. systemd's ordering is purely defined for jobs in
> queue. If your start job is ordered after another unit's start job,
> then it really will wait for the other start job to complete before
> dispatching yours. This may produce erroneous results if you don't
> configure the readiness of the service you wait on correctly, but that
> isn't a systemd issue at that point. Similarly, if a stop job for it
> gets enqueued, it will walk the reverse dependency and add one in
> queue for yours too, and yours will become runnable before the other
> one does, and as long as you're stopping, the other unit's stop job
> keeps waiting on you atleast (if nobody else) in queue.

to make it clear:

Type=simple won't work because that implies foreground process without
forking and systemd can't have any clue of the initialization state of
the service

perferred Type=notify which must be supported by the daemon or at least
Type=forking which needs the daemon to do a proper double fork *after*
it is ready to accept clietn tasks

at shutdown it's easier because the daemon only needs to exit with a
zero-state

that all is not really new and was not better with sysvinit, it only was
slow enough, full of sleep/usleep hacks and so most of the time by luck
worked but with no guarantess anyways
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-16 Thread Jonathon Kowalski
On Thu, Jan 17, 2019 at 4:49 AM Christopher Cox  wrote:
>
> 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.
>

This isn't right. systemd's ordering is purely defined for jobs in
queue. If your start job is ordered after another unit's start job,
then it really will wait for the other start job to complete before
dispatching yours. This may produce erroneous results if you don't
configure the readiness of the service you wait on correctly, but that
isn't a systemd issue at that point. Similarly, if a stop job for it
gets enqueued, it will walk the reverse dependency and add one in
queue for yours too, and yours will become runnable before the other
one does, and as long as you're stopping, the other unit's stop job
keeps waiting on you atleast (if nobody else) in queue.
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-16 Thread Christopher Cox

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 > 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@lists.freedesktop.org

https://lists.freedesktop.org/mailman/listinfo/systemd-devel





___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-16 Thread Christopher Cox

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.

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 > 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@lists.freedesktop.org

https://lists.freedesktop.org/mailman/listinfo/systemd-devel



___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2019-01-16 Thread Filipe Brandenburger
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.

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.

I hope this helps!

Cheers,
Filipe


On Wed, Jan 16, 2019 at 10:47 AM Christopher Cox 
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@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel
>
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel