1

I have a few services that I launch with systemd when I login in my user account, e.g. I did:

systemctl --user enable foo.service

But I usually terminate my X session abruptly, i.e. just pressing the power button to directly initiate systemd shutdown (I have a minimal desktop environment). Systemd terminates X sending all the child processes to the grave.

Will systemd gracefully shutdown the --user foo service in this case?

3

systemd --user runs as a system service called user@.service. This is a template service. Your instance might be user@1000.service. The number is your user ID (id -u).

The system shutdown procedure will signal user@1000.service to stop, with the usual signal SIGTERM. In response, the systemd --user process will signal each user service to stop.[*]

systemctl cat user@.service suggests there is little point setting a user service timeout (grace period) any longer than 120 seconds. After that, all the user services will recieve SIGKILL from the main system (PID 1). SIGKILL is immediately fatal.

To understand the settings in this file, see man systemd.kill, and also man systemd.service.


[*] Technically, what systemd --user does in response to SIGTERM is to activate the user unit exit.target. This is mentioned in man systemd. For more information about exit.target: systemctl cat exit.target will tell you to look in man systemd.special :-).

4
  • I don't have a desktop environment like GNOME or KDE, when pressing the power button my system launches systemctl shutdown which terminates X and all the child processes and I was wondering if it would terminate also the systemd user services in a bad way. Based on your answer I guess it does not. I have edited my question to clarify this point. Jul 1 '19 at 19:37
  • @FedericoSquartini hum. Technically, X clients don't have to die when they lose their connection to the server. But often, they don't have much better to do. systemd user services can potentially be X clients, but that doesn't mean they can't clean up carefully if X is just killed. Some of this can depend on how well the service program is written though - there are definitely some examples of "bad" programs over the years.
    – sourcejedi
    Jul 1 '19 at 19:47
  • My services are just servers, they do not depend on X. I guess the point is whether they are in the X process tree, login shell process tree, or not. I just printed the process tree, and it seems they are not, so everything should be fine and even after X has terminated the user services should still be alive until systemd stops them (likely immediately after). Jul 1 '19 at 19:51
  • 1
    @FedericoSquartini technically processes can be moved between systemd units. I mention this to say that systemctl status can show a tree of processes and which systemd unit they are in. (I.e. you don't have to pass a service name to systemctl status. If you don't pass a service name, it should show the full tree of all processes on the system).
    – sourcejedi
    Jul 1 '19 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.