Systemd是linux系统中用于管理开机服务的标准进程,用于控制Linux系统启动时运行的程序。它作为一个系统管理器,已经成为Linux操作系统的新标准。Systemd允许你创建自定义Systemd服务来运行和管理任何进程。在本文中,我们将解释如何在Linux上使用systemd管理服务.
翻译整理自:https://www.howtoforge.com/how-to-manage-systemd-services-with-systemctl/
前提条件
- 一个通过Systemd运行Linux的服务器,在这里使用ubuntu20.10。
- 在服务器上配置根密码。
验证systemd
默认情况下,Systemd预装在所有主要操作系统中。可以使用以下命令验证已安装的Systemd版本:
systemctl --version
你应该看到以下输出:
systemd 246 (246.6-1ubuntu1.1)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +ZSTD +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid
你可以使用以下命令找到Systemd的位置:
whereis systemd
你应该得到以下输出:
systemd: /usr/bin/systemd /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
要查找systemctl位置,请运行以下命令:
whereis systemctl
你应该得到以下输出:
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
你还可以使用以下命令验证Systemd是否正在运行:
ps -eaf | grep systemd
你应该得到以下输出:
root 307 1 1 12:28 ? 00:05:40 /lib/systemd/systemd-journald
root 328 1 0 12:28 ? 00:00:00 /lib/systemd/systemd-udevd
systemd+ 664 1 0 12:28 ? 00:00:03 /lib/systemd/systemd-resolved
systemd+ 665 1 0 12:28 ? 00:00:00 /lib/systemd/systemd-timesyncd
message+ 732 1 0 12:28 ? 00:00:05 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root 761 1 0 12:28 ? 00:00:01 /lib/systemd/systemd-logind
root 763 1 0 12:28 ? 00:00:02 /usr/sbin/thermald --systemd --dbus-enable --adaptive
你还可以使用以下命令分析每个进程在系统引导时所花费的时间:
systemd-analyze blame
你应该得到以下输出:
6.344s NetworkManager-wait-online.service
6.196s media-netshare.mount
1.477s apt-daily-upgrade.service
1.140s vmware.service
1.066s dev-sda3.device
526ms systemd-logind.service
493ms apt-daily.service
478ms systemd-journal-flush.service
324ms snapd.service
300ms udisks2.service
299ms systemd-udev-trigger.service
240ms accounts-daemon.service
239ms networkd-dispatcher.service
230ms vmware-USBArbitrator.service
224ms upower.service
204ms blueman-mechanism.service
183ms plymouth-read-write.service
164ms polkit.service
162ms avahi-daemon.service
161ms NetworkManager.service
使用以下命令分析启动时的关键链:
systemd-analyze critical-chain
你应该得到以下输出:
The time when unit became active or started is printed after the "@" character.
The time the unit took to start is printed after the "+" character.
graphical.target @14.518s
└─multi-user.target @14.518s
└─cpufrequtils.service @14.476s +41ms
└─loadcpufreq.service @14.429s +45ms
└─basic.target @1.702s
└─sockets.target @1.702s
└─snapd.socket @1.701s +762us
└─sysinit.target @1.697s
└─plymouth-read-write.service @1.513s +183ms
└─local-fs.target @1.509s
└─boot-efi.mount @1.474s +34ms
└─systemd-fsck@dev-disk-by\x2duuid-1365\x2dF283.service @1.438s +34ms
└─dev-disk-by\x2duuid-1365\x2dF283.device @1.437s
使用Systemd管理服务
Systemctl提供了一种更简单的方法来管理systemd服务。
要启动Apache服务,请运行以下命令:
systemctl start apache2
要重启Apache服务,请运行以下命令:
systemctl restart apache2
要停止Apache服务,请运行以下命令:
systemctl stop apache2
要重新加载Apache服务,请运行以下命令:
systemctl reload apache2
要使Apache服务在系统重新启动后启动,请运行以下命令:
systemctl enable apache2
要从系统启动中移除Apache服务,请运行以下命令:
systemctl disable apache2
要终止Apache服务,请运行以下命令:
systemctl kill apache2
要屏蔽Apache服务,请运行以下命令:
systemctl mask apache2
要取消对Apache进程的屏蔽,请运行以下命令:
systemctl unmask apache2
检查Apache服务是否启用,运行以下命令:
systemctl is-enabled apache2
要检查Apache服务的状态,请运行以下命令:
systemctl status apache2
如果Apache服务正在运行,则应获得以下输出:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-02-01 20:45:54 CST; 30s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 19777 (apache2)
Tasks: 55 (limit: 19041)
Memory: 6.6M
CGroup: /system.slice/apache2.service
├─19777 /usr/sbin/apache2 -k start
├─19778 /usr/sbin/apache2 -k start
└─19779 /usr/sbin/apache2 -k start
2月 01 20:45:54 hollowman-F117 systemd[1]: Starting The Apache HTTP Server...
2月 01 20:45:54 hollowman-F117 apachectl[19776]: AH00558: apache2: Could not reliably determin>
2月 01 20:45:54 hollowman-F117 systemd[1]: Started The Apache HTTP Server.
重命名或删除任何systemd文件后,需要重新加载该文件以应用更改。要重新加载systemd服务文件,请运行以下命令:
systemctl daemon-reload
管理systemd单元文件
以上命令对于管理单个服务非常有用。但是,它不能用来查找系统的当前状态。有许多其他命令可用于提供此信息。
要查看所有可用单元的列表,请运行以下命令:
systemctl list-unit-files
你应该在以下输出中看到所有单元文件的列表:
UNIT FILE STATE VENDOR PRESET
proc-sys-fs-binfmt_misc.automount static -
-.mount generated -
boot-efi.mount generated -
dev-hugepages.mount static -
dev-mqueue.mount static -
media-netshare.mount generated -
proc-sys-fs-binfmt_misc.mount disabled enabled
run-vmblock\x2dfuse.mount enabled enabled
sys-fs-fuse-connections.mount static -
sys-kernel-config.mount static -
sys-kernel-debug.mount static -
sys-kernel-tracing.mount static -
acpid.path enabled enabled
apport-autoreport.path enabled enabled
cups.path enabled enabled
systemd-ask-password-console.path static -
systemd-ask-password-plymouth.path static -
systemd-ask-password-wall.path static -
session-c4.scope transient -
accounts-daemon.service enabled enabled
acpid.service disabled enabled
alsa-restore.service static -
alsa-state.service static -
alsa-utils.service masked enabled
anacron.service enabled enabled
apache-htcacheclean.service disabled enabled
[email protected] disabled enabled
apache2.service enabled enabled
要查看所有活动单元的列表,请运行以下命令命令:
systemctl list-units
你应该请参见以下内容输出:
UNIT LOAD ACTIVE SUB DESCRIPTION >
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point>
sys-devices-pci0000:00-0000:00:01.0-0000:01:00.1-sound-card1.device loaded active plugged GP107GL High Definition Audio Controller >
sys-devices-pci0000:00-0000:00:02.0-drm-card0-card0\x2deDP\x2d1-intel_backlight.device loaded active plugged /sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/in>
sys-devices-pci0000:00-0000:00:14.0-usb1-1\x2d6-1\x2d6:1.0-bluetooth-hci0.device loaded active plugged /sys/devices/pci0000:00/0000:00:14.0/usb1/1-6/1-6:1.0/bluetoo>
sys-devices-pci0000:00-0000:00:17.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged LITEON_CV5-8Q256 1 >
sys-devices-pci0000:00-0000:00:17.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged LITEON_CV5-8Q256 2 >
sys-devices-pci0000:00-0000:00:17.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda3.device loaded active plugged LITEON_CV5-8Q256 3 >
sys-devices-pci0000:00-0000:00:17.0-ata1-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plugged LITEON_CV5-8Q256 >
sys-devices-pci0000:00-0000:00:17.0-ata2-host1-target1:0:0-1:0:0:0-block-sdb-sdb1.device loaded active plugged WDC_WD5000LPLX-22ZNTT0 软件 >
sys-devices-pci0000:00-0000:00:17.0-ata2-host1-target1:0:0-1:0:0:0-block-sdb-sdb2.device loaded active plugged WDC_WD5000LPLX-22ZNTT0 学习 >
sys-devices-pci0000:00-0000:00:17.0-ata2-host1-target1:0:0-1:0:0:0-block-sdb-sdb3.device loaded active plugged WDC_WD5000LPLX-22ZNTT0 娱乐 >
sys-devices-pci0000:00-0000:00:17.0-ata2-host1-target1:0:0-1:0:0:0-block-sdb.device loaded active plugged WDC_WD5000LPLX-22ZNTT0 >
sys-devices-pci0000:00-0000:00:1c.0-0000:02:00.0-net-enp2s0.device loaded active plugged RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller >
sys-devices-pci0000:00-0000:00:1c.4-0000:03:00.0-net-wlp3s0.device loaded active plugged Wireless 3165 (Dual Band Wireless AC 3165) >
sys-devices-pci0000:00-0000:00:1f.3-sound-card0.device loaded active plugged CM238 HD Audio Controller >
sys-devices-platform-serial8250-tty-ttyS0.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS0 >
查看在所有故障单元的列表中,运行以下命令命令:
systemctl --failed
列出所有服务包括已启用和已禁用的服务,请运行以下命令命令:
systemctl list-unit-files --type=service
你应该请参见以下内容输出:
UNIT FILE STATE VENDOR PRESET
accounts-daemon.service enabled enabled
acpid.service disabled enabled
alsa-restore.service static -
alsa-state.service static -
alsa-utils.service masked enabled
anacron.service enabled enabled
apache-htcacheclean.service disabled enabled
[email protected] disabled enabled
apache2.service enabled enabled
[email protected] disabled enabled
apparmor.service enabled enabled
apport-autoreport.service static -
[email protected] static -
apport.service generated -
apt-daily-upgrade.service static -
你可以查看SSH单元的属性,运行以下命令命令:
systemctl show sshd.service
你应该请参见以下输出:
Type=notify
Restart=on-failure
NotifyAccess=main
RestartUSec=100ms
TimeoutStartUSec=9s
TimeoutStopUSec=9s
TimeoutAbortUSec=9s
TimeoutStartFailureMode=terminate
TimeoutStopFailureMode=terminate
RuntimeMaxUSec=infinity
WatchdogUSec=0
WatchdogTimestampMonotonic=0
RootDirectoryStartOnly=no
RemainAfterExit=no
GuessMainPID=yes
RestartPreventExitStatus=255
MainPID=846
ControlPID=0
使用Systemctl管理套接字
你还可以使用Systemctl命令管理套接字。
要列出所有可用的系统套接字,请运行以下命令:
systemctl list-unit-files --type=socket
你应该看到以下内容输出:
UNIT FILE STATE VENDOR PRESET
acpid.socket enabled enabled
apport-forward.socket enabled enabled
avahi-daemon.socket enabled enabled
cups.socket enabled enabled
dbus.socket static -
saned.socket disabled enabled
snapd.socket enabled enabled
spice-vdagentd.socket static -
ssh.socket disabled enabled
syslog.socket static -
systemd-fsckd.socket static -
systemd-initctl.socket static -
systemd-journald-audit.socket static -
systemd-journald-dev-log.socket static -
启动SSH套接字,运行以下命令命令:
systemctl start ssh.socket
停止SSH套接字,运行以下命令命令:
systemctl stop ssh.socket
重新启动SSH套接字,运行以下命令命令:
systemctl restart ssh.socket
检查SSH套接字的状态,运行以下命令命令:
systemctl status ssh.socket
启用并在引导时禁用SSH套接字,运行以下命令:
systemctl enable ssh.socket
systemctl disable ssh.socket
Systemctl附加命令
可以使用systemctl命令执行多个任务。如果要将系统置于单用户模式,请运行以下命令:
systemctl rescue
停止系统,请运行以下命令命令:
systemctl halt
关闭系统,运行以下命令命令:
systemctl poweroff
重新启动系统,运行以下命令:
systemctl reboot