54

I know how to enable/disable lingering with loginctl.

But up to now I found no way to query the status of a user.

I want to know: Is lingering enable for user foo?

How can I access this information?

guettli
  • 3,833

3 Answers3

65

You can show a list of lingering users with

ls /var/lib/systemd/linger

because

loginctl enable-linger $USER
loginctl disable-linger $USER

do the equivalent of

touch /var/lib/systemd/linger/$USER
rm /var/lib/systemd/linger/$USER
Markus Kuhn
  • 1,001
26

loginctl user-status foo shows linger status.

  • 4
    I am unsure if this really works. Some minutes ago this printed a tree for a test-user which has not lingering enabled. Now, some minutes later, the output is "Failed to get user: No user '1003' known or logged in". I will use the solution from Markus Kuhn (check if file /var/lib/systemd/linger/$USER exists) – guettli Aug 15 '17 at 09:05
20

The best I found for check it in scripts (programmatically):

loginctl show-user "$USER" --property=Linger | grep -q 'yes'
Robert Siemer
  • 551
  • 9
  • 20
Xorax
  • 358
  • Why not use this: "check the existance of /var/lib/systemd/linger"? – guettli Jan 04 '19 at 09:30
  • 11
    Because it's more subject to changes without notice. – Xorax Jan 08 '19 at 14:31
  • Unfortunately, this doesn't work on systemd 248.3-1ubuntu8.6 on my Ubuntu 21.10 install. loginctl show-user doesn't show me any property that has the word "linger" in it. And loginctl user-status shows me the linger status at the top, but does not support --property= for filtering. But the ls answer works, so looks like that's what I'll have to use. – Cliff Jun 29 '22 at 03:11
  • 2
    @Cliff I had the same issue until I noticed loginctl show-user and loginctl show-user $USER give completely different outputs. The latter shows Linger. – chutz Dec 29 '22 at 05:51
  • Notice that the show-user command might fail like "Failed to get user: User ID 1001 is not logged in or lingering" when testing for a different user than the main user logged user (after running loginctl enable-linger otheruser then the show-user command no longer fails) – hlovdal Sep 28 '23 at 12:55