0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

定時帰る術-Linuxサービス管理(systemctlコマンド)

Posted at

failedサービスを表示される

[root@test tmp]# systemctl --failed
  UNIT                  LOAD   ACTIVE SUB    DESCRIPTION
● dnf-makecache.service loaded failed failed dnf makecache

自動起動になっているサービス確認

[root@test tmp]# systemctl list-unit-files | grep enabled
atd.service                                enabled
auditd.service                             enabled
autovt@.service                            enabled
crond.service                              enabled
dbus-org.freedesktop.nm-dispatcher.service enabled
dbus-org.freedesktop.timedate1.service     enabled
getty@.service                             enabled
import-state.service                       enabled
irqbalance.service                         enabled
iscsi-onboot.service                       enabled
iscsi.service                              enabled
libstoragemgmt.service                     enabled
loadmodules.service                        enabled
lvm2-monitor.service                       enabled
mcelog.service                             enabled
mdmonitor.service                          enabled
microcode.service                          enabled
multipathd.service                         enabled

chronydを自動起動にし、起動する

[root@test tmp]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)


[root@test tmp]# systemctl enable --now chronyd
Created symlink /etc/systemd/system/multi-user.target.wants/chronyd.service → /usr/lib/systemd/system/chronyd.service.
[root@test tmp]#
[root@test tmp]#
[root@test tmp]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2023-10-18 14:18:28 JST; 3s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 2133 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 2130 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 2132 (chronyd)
    Tasks: 1 (limit: 24895)
   Memory: 352.0K
   CGroup: /system.slice/chronyd.service
           mq2132 /usr/sbin/chronyd

サービス起動、停止、再起動

[root@test tmp]# systemctl start chronyd
[root@test tmp]# systemctl stop chronyd
[root@test tmp]# systemctl restart chronyd

サービスmask

maskにしてしまうと、startしようとしてもできない
(disableのパワーアップ版)

[root@test tmp]# systemctl mask chronyd
Created symlink /etc/systemd/system/chronyd.service → /dev/null.
[root@test tmp]# systemctl start chronyd
Failed to start chronyd.service: Unit chronyd.service is masked.

サービス再起動と再読み込み

restart:PIDが変わってしまう
(restartは名前の通り、activeのサービスに対してはstopして、startする。activeではないサービスに対しては、startだけする)
reload:定義ファイルだけ再読み込み、PIDが変わらない

[root@test tmp]#  systemctl status httpd | grep PID
 Main PID: 2203 (httpd)
[root@test tmp]#
[root@test tmp]#
[root@test tmp]#
[root@test tmp]#
[root@test tmp]#
[root@test tmp]# systemctl restart httpd
[root@test tmp]# systemctl status httpd | grep PID
 Main PID: 2437 (httpd)
[root@test tmp]#
[root@test tmp]#
[root@test tmp]#
[root@test tmp]# systemctl reload httpd
[root@test tmp]# systemctl status httpd | grep PID
 Main PID: 2437 (httpd)

サービス状態確認(statusじゃない)

activeになっているか

[root@test tmp]# systemctl is-active sshd
active

起動起動になっているか

[root@test tmp]# systemctl is-enabled sshd
enabled

サービスのxxx.serviceファイル内容を表示される

[root@test tmp]# systemctl cat sshd
# /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target

[Service]
Type=notify
EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

target確認

rescue.target:シングルモード
(厳密でいいとシングルではない、NetworkManagerが起動可能で、sshdも起動しておれば、sshもアクセスできる)

multi-user.target:rescue.targetで、ならにNetworkManagerなどのサービスも起動される。

graphical.target:multi-user.target、ならにGUIも自動起動される。(インストールが必要)

[root@test dap80002]# systemctl get-default
multi-user.target
rescue.targetにする。(有効するのには、OS再起動が必要)
[root@test ~]#systemctl set-default rescue.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/rescue.target

daemon-reload:全サービスファイル再読み込み

コンパイルで.serviceファイルを手動で作成する場合、daemon-reloadしない、読み込めません。

[root@test ~]# systemctl daemon-reload
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?