Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Linux - Software: Service

Back


Service


systemd


Unit File

Directory Created Precedence
/usr/lib/systemd/system/ By installed RPM packages. -
/run/systemd/system/ At run time. over installed service unit files.
/etc/systemd/system/ by systemctl enable and added for extending a service over runtime unit files.
Unit Type File Extension Description
Service unit .service A system service.
Target unit .target A group of systemd units.
Automount unit .automount A file system automount point.
Device unit .device A device file recognized by the kernel.
Mount unit .mount A file system mount point.
Path unit .path A file or directory in a file system.
Scope unit .scope An externally created process.
Slice unit .slice A group of hierarchically organized units that manage system processes.
Snapshot unit .snapshot A saved state of the systemd manager.
Socket unit .socket An inter-process communication socket.
Swap unit .swap A swap device or a swap file.
Timer unit .timer A systemd timer.

# get unit status for a specific system unit
systemctl list-units | grep firewalld

# get all unit file enabled at boot.
systemctl list-unit-files | grep enabled
# get all unit file disabled at boot.
systemctl list-unit-files | grep disabled
# get all unit file sattic at boot.
systemctl list-unit-files | grep sattic
# get all unit file masked at boot.
systemctl list-unit-files | grep masked

systemctl and service Command


systemd Management

CMD DESC
systemctl --version Used to check if the systemd installed
ps -ef \| grep systemd Used to check if the systemd running
systemctl --all / systemctl list-units Display all available systemd units
systemctl list-unit-files List all unit files

# shows installed software packages and their versions.
rpm -qa
rpm -qa | wc -l

# shows the status of services and other systemd units
systemctl --all


Unit Management

CMD DESC
systemctl / systemctl list-units Lists active units(in memory) and current states
systemctl list-units -a Lists all units, active + inactive
systemctl list-units -t socket Lists all socke type unit
systemctl list-units --failed Lists all failed unit at last system boot
systemctl list-unit-files Lists unit files and states
systemctl list-sockets Lists units of type socket
systemctl list-dependencies Lists dependency tree for all unit
systemctl list-dependencies crond Lists dependency tree for a unit
systemctl show Show all properties
systemctl show crond Show the service’s properties

Service Units Management

CMD DESC
systemctl list-units -t service Lists all serivces type unit
systemctl daemon-reload Re-reads and reloads all unit configuration files
systemctl status crond Check the status of a service
systemctl is-enabled crond Check if a service is enabled
systemctl is-active crond Checks whether a unit is running
systemctl is-failed crond Checks whether a unit is in the failed state
systemctl start crond Start a service
systemctl stop crond Stop a service
systemctl reload crond Reload the configuration of a service
systemctl restart crond Stop and Start a service after a configuration change
systemctl enable crond Enable a service at boot time
systemctl disable crond Disable a service at boot time
systemctl mask crond Disable a service completely including dependencies
systemctl unmask crond Enable a service completely including dependencies
systemctl kill unit_name Terminates all processes for a unit

Target Unit Management

CMD DESC
systemctl list-units -t target Lists all target type unit
systemctl list-units -t target --state active Get the current ative target units
systemctl get-default Shows the default boot target
systemctl set-default graphical.target Set the system to boot into a graphical environment.
systemctl isolate rescue.target Switch to Rescue Mode (Start one unit and stop all others)
systemctl reboot Reboots a Linux system
systemctl poweroff Shut down the system

Lab: Change target

Default target

systemctl get-default
# graphical.target

systemctl set-default multi-user
# Removed "/etc/systemd/system/default.target".
# Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/multi-user.target.

systemctl set-default graphical.target
Removed "/etc/systemd/system/default.target".
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/graphical.target.

Switch currrent target

# To switch into multi-user
systemctl isolate multi-user
# To return to the graphical target:
systemctl isolate graphical


Start/Stop a service

Start a Service


Stop a Service


Enable/Disable a Service

Enable a Service


Disable a Service


Mask/Unmask a Service

Mask a Service


Unmask a Service


Lab: Manage nginx

Install nginx Package

su -

dnf update -y
dnf upgrade -y
dnf install nginx -y

# confirm
rpm -qa | grep nginx
# nginx-mod-http-image-filter-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
# nginx-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
# nginx-mod-http-xslt-filter-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
# nginx-mod-stream-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
# nginx-filesystem-1.14.1-9.module+el8.0.0+4108+af250afe.noarch
# nginx-mod-mail-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
# nginx-all-modules-1.14.1-9.module+el8.0.0+4108+af250afe.noarch
# nginx-mod-http-perl-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
systemctl list-unit-files | grep nginx
# nginx.service                              disabled

Start/Stop nginx

systemctl show nginx
# Type=forking
# Restart=no
# PIDFile=/run/nginx.pid
# NotifyAccess=none
# RestartUSec=100ms
# TimeoutStartUSec=1min 30s
# TimeoutStopUSec=5s
# RuntimeMaxUSec=infinity
# WatchdogUSec=0
# ...

systemctl --all | grep nginx
# none
systemctl start nginx
# display status
systemctl status nginx
# ● nginx.service - The nginx HTTP and reverse proxy server
#    Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
#    Active: active (running) since Sun 2024-12-01 00:46:26 EST; 1min 26s ago
#   Process: 73366 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
#   Process: 73364 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
#   Process: 73362 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
#  Main PID: 73367 (nginx)
#     Tasks: 9 (limit: 22878)
#    Memory: 16.6M
#    CGroup: /system.slice/nginx.service
#            ├─73367 nginx: master process /usr/sbin/nginx
#            ├─73368 nginx: worker process
#            ├─73369 nginx: worker process
#            ├─73370 nginx: worker process
#            ├─73371 nginx: worker process
#            ├─73372 nginx: worker process
#            ├─73373 nginx: worker process
#            ├─73374 nginx: worker process
#            └─73375 nginx: worker process

# Dec 01 00:46:26 rhelhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server.>
# Dec 01 00:46:26 rhelhost.localdomain nginx[73364]: nginx: the configuration file /etc/nginx/nginx.>
# Dec 01 00:46:26 rhelhost.localdomain nginx[73364]: nginx: configuration file /etc/nginx/nginx.conf>
# Dec 01 00:46:26 rhelhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
systemctl --all | grep nginx
# nginx.service    loaded    active   running   The nginx HTTP and reverse proxy server
# verify
curl 127.0.0.1
# return the default nginx website

# stop service
systemctl stop nginx
# display status
systemctl status nginx
# ● nginx.service - The nginx HTTP and reverse proxy server
#    Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
#    Active: inactive (dead)

# Dec 01 00:36:22 rhelhost.localdomain systemd[1]: nginx.service: Unit cannot be reloaded because it>
# Dec 01 00:46:26 rhelhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server.>
# Dec 01 00:46:26 rhelhost.localdomain nginx[73364]: nginx: the configuration file /etc/nginx/nginx.>
# Dec 01 00:46:26 rhelhost.localdomain nginx[73364]: nginx: configuration file /etc/nginx/nginx.conf>
# Dec 01 00:46:26 rhelhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
# Dec 01 00:56:03 rhelhost.localdomain systemd[1]: Stopping The nginx HTTP and reverse proxy server.>
# Dec 01 00:56:03 rhelhost.localdomain systemd[1]: nginx.service: Succeeded.
# Dec 01 00:56:03 rhelhost.localdomain systemd[1]: Stopped The nginx HTTP and reverse proxy server.
systemctl --all | grep nginx
# none
curl 127.0.0.1
# curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused


Enable/Disable nginx

# Check link before operations
ll /etc/systemd/system/multi-user.target.wants | grep nginx*
# none
ll /etc/systemd/system/ | grep nginx*
# drwxr-xr-x. 2 root root    6 Aug 30  2019 nginx.service.d

systemctl enable nginx.service
# Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
systemctl is-enabled nginx.service
# enabled
systemctl list-unit-files | grep nginx.service
# nginx.service                              enabled
ll /etc/systemd/system/multi-user.target.wants | grep nginx*
# lrwxrwxrwx. 1 root root 37 Dec  1 00:38 nginx.service -> /usr/lib/systemd/system/nginx.service
ll /etc/systemd/system/ | grep nginx*
# drwxr-xr-x. 2 root root    6 Aug 30  2019 nginx.service.d

systemctl disable nginx.service
# Removed /etc/systemd/system/multi-user.target.wants/nginx.service.
systemctl is-enabled nginx.service
# disabled
systemctl list-unit-files | grep nginx.service
# nginx.service                              disabled
ll /etc/systemd/system/multi-user.target.wants | grep nginx*
# none
ll /etc/systemd/system/ | grep nginx*
# drwxr-xr-x. 2 root root    6 Aug 30  2019 nginx.service.d

Mask/Unmask nginx

systemctl mask nginx.service
# Created symlink /etc/systemd/system/nginx.service → /dev/null.
systemctl list-unit-files | grep nginx.service
# nginx.service                              masked
ll /etc/systemd/system/multi-user.target.wants | grep nginx*
# none
ll /etc/systemd/system/ | grep nginx*
# lrwxrwxrwx. 1 root root    9 Dec  1 00:41 nginx.service -> /dev/null
# drwxr-xr-x. 2 root root    6 Aug 30  2019 nginx.service.d

systemctl unmask nginx.service
# Removed /etc/systemd/system/nginx.service.
systemctl list-unit-files | grep nginx.service
# nginx.service                              disabled
ll /etc/systemd/system/multi-user.target.wants | grep nginx*
# none
ll /etc/systemd/system/ | grep nginx*
# drwxr-xr-x. 2 root root    6 Aug 30  2019 nginx.service.d

Clear up

dnf remove -y nginx
ll /etc/systemd/system/ | grep nginx*
# none

Service Log

journalctl -u service_name

TOP