systemctl command with all possible options rhel7 and centos7

systemctl is a command available in new version of Linux. Systemctl is used to control the systemd and service manager. To control services we have to use lot many options along with systemctl command. It is changed dramatically in new version of Linux. In this article we are going to explore as many possible ways as to use systemctl command in Linux.

Let’s start with checking the service status.

1. Service Status Check and show service units

below is the command used to verify the service status

[root@TechTutorial ~]# systemctl status network.service
network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network)
   Active: active (exited) since Thu 2016-04-28 13:39:38 IST; 32min ago
  Process: 5239 ExecReload=/etc/rc.d/init.d/network reload (code=killed, signal=TERM)
  Process: 5313 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)
[root@server ~]# systemctl show crond.service
Id=crond.service
Names=crond.service
Requires=basic.target
Wants=system.slice
Conflicts=shutdown.target
Before=shutdown.target

2. Verify service is active and enabled

If service is active it means service is running without any issues.  As a example we verify web service is running OR not.

[root@TechTutorial ~]# systemctl is-active httpd.service
active

Enabling service means we are ensuring that service should start when server is rebooted. In older versions chkconfig command.

[root@server ~]# systemctl enable crond.service
[root@server ~]# systemctl is-enabled crond.service
enabled

systemctl is-enabled

3. Start and restart service using systemctl

As simple as starting the services and restarting the services is very easy, Here we have little more than that in New version.

[root@server ~]# systemctl start crond.service
[root@server ~]# systemctl restart crond.service

as we say it is more than start and restart we have systemctl try-restart now your thinking about what is the difference between restart and try-restart..?

option restart will restart the service if it is in stopped state also.

option try-restart Restart one or more units specified on the command line if the units are running. This does nothing if units are not running. Note that, for compatibility with Red Hat init scripts, condrestart is equivalent to this command.

[root@server ~]# systemctl try-restart crond.service

4. Listing dependencies, jobs, sockets, unit-files and Units

Listing dependencies means what are the services we have to start before starting this required service this before version this feature was not there.

Shows required and wanted units of the specified unit. If no unit is specified, default.target is implied. Target units are recursively expanded. When –all is passed, all other units are recursively expanded as well

[root@server ~]# systemctl list-dependencies crond.service
crond.service
├─system.slice
└─basic.target
 ├─alsa-restore.service
 ├─alsa-state.service
 ├─firewalld.service
 ├─microcode.service
 ├─rhel-autorelabel-mark.service
 ├─rhel-autorelabel.service
 ├─rhel-configure.service
 ├─rhel-dmesg.service
 ├─rhel-loadmodules.service
 ├─paths.target
 ├─slices.target

list-jobs will show what are jobs running currently in background

[root@server ~]# systemctl list-jobs
No jobs running.

Listing installed unit files

[root@server ~]# systemctl list-unit-files |grep sshd
anaconda-sshd.service static
sshd-keygen.service static
sshd.service enabled
sshd@.service static
sshd.socket disabled

List all available sockets

[root@server ~]# systemctl list-sockets
LISTEN UNIT ACTIVATES
/dev/initctl systemd-initctl.socket systemd-initctl.service
/dev/log systemd-journald.socket systemd-journald.service
/run/dmeventd-client dm-event.socket dm-event.service
/run/dmeventd-server dm-event.socket dm-event.service
/run/lvm/lvmetad.socket lvm2-lvmetad.socket lvm2-lvmetad.service
/run/systemd/journal/socket systemd-journald.socket systemd-journald.service
/run/systemd/journal/stdout systemd-journald.socket systemd-journald.service
/run/systemd/shutdownd systemd-shutdownd.socket systemd-shutdownd.service
/run/udev/control systemd-udevd-control.socket systemd-udevd.service
/var/run/avahi-daemon/socket avahi-daemon.socket avahi-daemon.service
/var/run/cups/cups.sock cups.socket cups.service
/var/run/dbus/system_bus_socket dbus.socket dbus.service
/var/run/rpcbind.sock rpcbind.socket rpcbind.service
@ISCSIADM_ABSTRACT_NAMESPACE iscsid.socket iscsid.service
@ISCSID_UIP_ABSTRACT_NAMESPACE iscsiuio.socket iscsiuio.service
kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service

16 sockets listed.
Pass --all to see loaded but inactive sockets, too.

Note: because the addresses might contains spaces, this output is not suitable for programmatic consumption.

[root@server ~]# systemctl list-sockets --show-types
LISTEN TYPE UNIT ACTIVATES
/dev/initctl FIFO systemd-initctl.socket systemd-initctl.service
/dev/log Datagram systemd-journald.socket systemd-journald.service
/run/dmeventd-client FIFO dm-event.socket dm-event.service
/run/dmeventd-server FIFO dm-event.socket dm-event.service
/run/lvm/lvmetad.socket Stream lvm2-lvmetad.socket lvm2-lvmetad.service
/run/systemd/journal/socket Datagram systemd-journald.socket systemd-journald.service
/run/systemd/journal/stdout Stream systemd-journald.socket systemd-journald.service
/run/systemd/shutdownd Datagram systemd-shutdownd.socket systemd-shutdownd.service
/run/udev/control SequentialPacket systemd-udevd-control.socket systemd-udevd.service
/var/run/avahi-daemon/socket Stream avahi-daemon.socket avahi-daemon.service
/var/run/cups/cups.sock Stream cups.socket cups.service
/var/run/dbus/system_bus_socket Stream dbus.socket dbus.service
/var/run/rpcbind.sock Stream rpcbind.socket rpcbind.service
@ISCSIADM_ABSTRACT_NAMESPACE Stream iscsid.socket iscsid.service
@ISCSID_UIP_ABSTRACT_NAMESPACE Stream iscsiuio.socket iscsiuio.service
kobject-uevent 1 Netlink systemd-udevd-kernel.socket systemd-udevd.service

16 sockets listed.
Pass --all to see loaded but inactive sockets, too.

[root@server ~]# systemctl list-sockets --failed
0 sockets listed.
Pass --all to see loaded but inactive sockets, too.

5. Setting up default target (Older version Run Level) and getting default target

We have to use set-default to set default run level and we can see default run level using get-default option as shown below example

[root@server ~]# systemctl set-default multi-user.target
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/default.target'


[root@server ~]# systemctl get-default
multi-user.target

6. Masking service ans unmasking service

What is mean by masking service, there is situation that company will have multiple administrators working together still there are times one administrator will stop the service which is not required but another administrator will start the same service unfortunately Or unknowingly , will lead to lot of problems to avoid this types of situations. We have to disable, stop the service and mask it, when other administrator try to start also the service will never start until unless explicitly unmask.

[root@server ~]# systemctl disable crond.service
rm '/etc/systemd/system/multi-user.target.wants/crond.service'
[root@server ~]# systemctl stop crond.service
[root@server ~]# systemctl mask crond.service
ln -s '/dev/null' '/etc/systemd/system/crond.service'
[root@server ~]# systemctl status crond.service
crond.service
 Loaded: masked (/dev/null)
 Active: inactive (dead)

As shown in above command examples we have stopped the service, disabled the service and masked the service. Now try to start the service.

[root@server ~]# systemctl start crond.service
Failed to issue method call: Unit crond.service is masked.

now unmask the service and start it will start

[root@server ~]# systemctl unmask crond.service
rm '/etc/systemd/system/crond.service'
[root@server ~]# systemctl start crond.service
[root@server ~]# systemctl status crond.service
crond.service - Command Scheduler
 Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled)
 Active: active (running) since Thu 2016-04-28 22:45:12 IST; 9s ago
 Main PID: 7521 (crond)
 CGroup: /system.slice/crond.service
 └─7521 /usr/sbin/crond -n

7. Reload and reset service status

Most of the administrator will still have an question that what is the difference between service reload and restart.

Service reload is used whenever we changed something to the service and we would like to push the changes to the service without interrupting the connected users. Reloading the service will never change existing PID (Process Identity)

Service restart is used to restart the service which means stop and start the service, whenever we run restart existing users will disconnect and new PID will be created. It required little downtime to the service changes.

Before reloading the service status

[root@server ~]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
 Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
 Active: active (running) since Thu 2016-04-28 05:56:52 IST; 16h ago
 Process: 7228 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 1601 (sshd)
 CGroup: /system.slice/sshd.service
 └─1601 /usr/sbin/sshd -D

After reloading the service

[root@server ~]# systemctl reload sshd.service
[root@server ~]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
 Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
 Active: active (running) since Thu 2016-04-28 05:56:52 IST; 16h ago
 Process: 7243 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 1601 (sshd)
 CGroup: /system.slice/sshd.service
 └─1601 /usr/sbin/sshd -D

if observe correctly PID before and after reload not changed.

Reset the “failed” state of the specified units

[root@server ~]# systemctl reset-failed sshd.service

8. Daemon reload option

After deleting the file or directory, you should reload the systemd process so that it no longer attempts to reference these files and reverts back to using the system copies. You can do this by typing:

[root@server ~]# systemctl daemon-reload

9. Isolating Targets using systemctl command

It is possible to start all of the units associated with a target and stop all units that are not part of the dependency tree. The command that we need to do this is called, appropriately, isolate. This is similar to changing the runlevel in other init systems.

For instance, if you are operating in a graphical environment with graphical.target active, you can shut down the graphical system and put the system into a multi-user command line state by isolating the multi-user.target. Since graphical.target depends on multi-user.target but not the other way around, all of the graphical units will be stopped.

You may wish to take a look at the dependencies of the target you are isolating before performing this procedure to ensure that you are not stopping vital services:

systemctl list-dependencies multi-user.target

When you are satisfied with the units that will be kept alive, you can isolate the target by typing:

systemctl isolate multi-user.target

10. Create service snapshot and delete

Create a snapshot. If a snapshot name is specified, the new snapshot will be named after it. If none is specified, an automatic snapshot name is generated. In either case, the snapshot name used is printed to STDOUT, unless –quiet is specified.

A snapshot refers to a saved state of the systemd manager. It is implemented itself as a unit that is generated dynamically with this command and has dependencies on all units active at the time. At a later time, the user may return to this state by using the isolate command on the snapshot unit.

Snapshots are only useful for saving and restoring which units are running or are stopped, they do not save/restore any other state. Snapshots are dynamic and lost on reboot.

[root@server ~]# systemctl snapshot sshd.service
sshd.service.snapshot
[root@server ~]# systemctl status sshd.service.snapshot
sshd.service.snapshot
 Loaded: loaded
 Active: inactive (dead)

[root@server ~]# systemctl delete sshd.service.snapshot
[root@server ~]# systemctl status sshd.service.snapshot
sshd.service.snapshot
 Loaded: not-found (Reason: No such file or directory)
 Active: inactive (dead)

Using Shortcuts for Important Events

Conclusion

Now you know how to use some of the systemctl commands to start, stop, restart, mask , unmask and enable systemd services.

 

We are waiting for your valuable comments dear, Don’t wait write your feedback.

 

Search Engine Keywords :

systemd command in linux,  systemctl command not found in linux, systemctl command in linux,  systemctl command not found linux,  systemctl command not found linux mint,  what is systemctl command in linux,  sudo systemctl command not found,  ubuntu systemctl command not found,  systemctl list running services,  systemctl is enabled,  systemctl mask,  centos systemctl command not found,  systemctl list services,  systemctl status,  install systemctl ubuntu, systemctl command not found centos,  linux systemctl command, systemctl add service, systemctl start up, systemctl disable service, systemctl commands, centos 7 systemctl add service, centos 7 service command, rhel 7 systemctl, centos 7 systemctl, bash sudo not found, bash visudo command not found, bash sudo command not found, rosrun command not found, systemctl status degraded, bash systemctl command not found, systemctl command not found ubuntu, systemctl enable service, systemctl create new service, systemctl command in linux, bash command not found linux, bash open command not found, command not found bash script, install systemctl, ubuntu upstart systemd, does ubuntu use systemd,  ubuntu 14.04 systemd,  systemd ubuntu,  systemd how to,  systemctl linux,  debian systemctl,  centos systemctl,  systemctl command not found,  systemctl service file,  systemctl stop iptables, systemctl remove service,  systemctl stop,  systemctl service file location,  systemctl set default,  systemctl is enabled static

Thanks for your wonderful Support and Encouragement

Ravi Kumar Ankam

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

Leave a Reply

Your email address will not be published. Required fields are marked *