systemd : how to get the running target

24,466

Solution 1

In systemd, there may be more than one active target at a time.

To inspect the list of all currently active targets:

systemctl list-units --type target --state active

To quickly find out whether a specific target (e.g. user-defined.target) is active or not:

systemctl is-active user-defined.target

Solution 2

There is no systemd command to query the running target or the last target used with isolate.

systemd does ship with a command called runlevel for compatibility for older systems. This will prevent the current "runlevel". The concept is obsolete, but as seen as man runlevel, particular run levels map to particular systemd targets. This command might be helpful as long as standard targets are used. It would not be useful if a custom target was used which did not map to a legacy runlevel.

More discussion about workarounds is on a [https://www.centos.org/forums/viewtopic.php?t=54347](CentOS forum).

Solution 3

Here are some screenshots of systemctl list-units --type target after pursuing some targets.

emergency target screenshot
emergency target

rescue target screenshot
rescue target

multi-user target screenshot
multi-user target

Solution 4

Similarly to the answer previously mentioned you can use:

systemctl list-units --type target | egrep "eme|res|gra|mul" | head -1

What you get as a result is your current target.

If you have installed unit that has in its name one of these four strings above, you could add the ^ character in front of them - egrep "^eme|^res|^gra|^mul"

After I've read 'Eduard Rozenberg' post below I decided to edit my answer, actually to provide additional clarification. I don't know if Eduard tried my solution before posting his answer.
However, if you use this command above you should get right results. Eduard states that he gets both, graphical and multi-user target. We should get both, graphical and multi-user targets only in GUI environment because graphical.target wants multi-user.target.
Head successfully resolves this showing only first line (graphical.target), because results are sorted alphabetically.

@Eduard Rozenberg - if you read this please provide feedback if this command works for you, thanks...

Also I would like to provide information for shorter command/typing:

  • you can use -t instead of --type
Share:
24,466

Related videos on Youtube

iamauser
Author by

iamauser

Updated on September 18, 2022

Comments

  • iamauser
    iamauser over 1 year
    • Command systemctl get-default returns user-defined.target.
    • Then I use systemctl isolate multi-user.target to switch to multi-user.target.
    • I can see that a bunch of services that shouldn't be running on user-defined.taret and should be running on multi-user are running, this implies I am on multi-user.target.
    • But, systemctl get-default always returns user-defined.target.

    Question is, without looking and sorting through the services, how do I know that I am running on multi-user.target after using isolate ?

    • Admin
      Admin about 7 years
      There isn't a single target systemctl list-units --type target
    • Admin
      Admin about 7 years
      Not entirely sure what you mean. I know there isn't a single target, but then why the default and isolate? What do they represent ?
    • Admin
      Admin about 7 years
      @iamauser. isolate means change to the specified target. That does not survive a reboot unless you persist the specified target using set-default
  • Bachsau
    Bachsau almost 4 years
    systemd is, in fact, stateless. There is no current "runlevel", as you can start and stop units all the time. This is why these units are called "targets" and the command is called "isolate".
  • ash
    ash over 3 years
    systemd doesn't have the concept of runlevels and doesn't come with a runlevel command. Your runlevel command is part of another init system left on your computer and probably won't reflect the current state of your computer.