systemd : how to get the running target
- Command
systemctl get-default
returnsuser-defined.target
. - Then I use
systemctl isolate multi-user.target
to switch tomulti-user.target
. - I can see that a bunch of services that shouldn't be running on
user-defined.taret
and should be running onmulti-user
are running, this implies I am onmulti-user.target
. - But,
systemctl get-default
always returnsuser-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
?
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
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).
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