Wednesday, November 18, 2015

How To Use Systemctl to Manage Systemd Services in RHEL7

Useful SystemD commands (hints for systemctl or systemctl vs chkconfig and service)
List all running services



# systemctl

Start/stop or enable/disable services

Activates a service immediately:

# systemctl start foo.service

Deactivates a service immediately:

# systemctl stop foo.service

Restarts a service:

# systemctl restart foo.service

Shows status of a service including whether it is running or not:

# systemctl status foo.service

Enables a service to be started on bootup:

# systemctl enable foo.service

Disables a service to not start during bootup:

# systemctl disable foo.service

Check whether a service is already enabled or not:

# systemctl is-enabled foo.service; echo $?

0 indicates that it is enabled. 1 indicates that it is disabled

How do I change the runlevel?

systemd has the concept of targets which is a more flexible replacement for runlevels in sysvinit.

Run level 3 is emulated by multi-user.target. Run level 5 is emulated by graphical.target.
 runlevel3.target is a symbolic link to multi-user.target and runlevel5.target is a symbolic link to graphical.target.

You can switch to ‘runlevel 3′ by running

# systemctl isolate multi-user.target (or) systemctl isolate runlevel3.target

You can switch to ‘runlevel 5′ by running

# systemctl isolate graphical.target (or) systemctl isolate runlevel5.target

How do I change the default runlevel?

systemd does not use /etc/inittab file.
List the current run level


# systemctl list-units --type=target

[root@redhat7 ~]# systemctl list-units --type target
UNIT                LOAD   ACTIVE SUB    DESCRIPTION
basic.target        loaded active active Basic System
cryptsetup.target   loaded active active Encrypted Volumes
getty.target        loaded active active Login Prompts
graphical.target    loaded active active Graphical Interface
local-fs-pre.target loaded active active Local File Systems (Pre)
local-fs.target     loaded active active Local File Systems
multi-user.target   loaded active active Multi-User System
network.target      loaded active active Network
nfs.target          loaded active active Network File System Server
paths.target        loaded active active Paths
remote-fs.target    loaded active active Remote File Systems
slices.target       loaded active active Slices
sockets.target      loaded active active Sockets
swap.target         loaded active active Swap
sysinit.target      loaded active active System Initialization
timers.target       loaded active active Timers

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.


runlevel command still works with systemd. You can continue using that however runlevels is a legacy concept in systemd and is emulated via ‘targets’ and multiple targets can be active at the same time.

If u want to change default runlevel by using following command u can change the default runlevel in rhel7

[root@rhel7 ~]# systemctl set-default multi-user.target

[root@rhel7 ~]# reboot or systemctl reboot

You are server defaulty goes to cli mode.

if u want graphical mode using following you can change the cli to graphical mode

[root@rhel7 ~]# systemctl set-default graphical.target

[root@rehl7 ~]# reboot or systemctl reboot or init 6

you are default server is goes to graphical mode.



Service vs. systemd

# service NetworkManager stop

(or)

# systemctl stop NetworkManager.service

Chkconfig vs. systemd

# chkconfig NetworkManager off

(or)

# systemctl disable NetworkManager.service

Readahead

systemd has a built-in readahead implementation is not enabled on upgrades. It should improve bootup speed but your mileage may vary depending on your hardware. To enable it:

# systemctl enable systemd-readahead-collect.service
# systemctl enable systemd-readahead-replay.service



How to check the number of service running on the you are server in rhel7?

using following command you can check number of services running on you are server

[root@rhel7~]# systemctl list-units --type service




SystemD cheatsheet


RHEL6                          RHEL7

service foobar start     systemctl start foobar.service             Used to start a service (not reboot persistent)
service foobar stop     systemctl stop foobar.service             Used to stop a service (not reboot persistent)
service foobar restart     systemctl restart foobar.service     Used to stop and then start a service
service foobar reload     systemctl reload foobar.service     When supported, reloads the config file without interrupting pending operations.
service foobar condrestart     systemctl condrestart foobar.service     Restarts if the service is already running.
service foobar status     systemctl status foobar.service     Tells whether a service is currently running.
ls /etc/rc.d/init.d/     ls /lib/systemd/system/*.service /etc/systemd/system/*.service     Used to list the services that can be started or stopped
chkconfig foobar on     systemctl enable foobar.service     Turn the service on, for start at next boot, or other trigger.
chkconfig foobar off     systemctl disable foobar.service     Turn the service off for the next reboot, or any other trigger.
chkconfig foobar     systemctl is-enabled foobar.service     Used to check whether a service is configured to start or not in the current environment.
chkconfig foobar –list     ls /etc/systemd/system/*.wants/foobar.service     Used to list what levels this service is configured on or off
chkconfig foobar –add           Not needed, no equivalent.

No comments:

Post a Comment