如何在 CentOS 7.x 或 RHEL Linux 7 上使用 systemctl
在本文中,我们将学习如何使用 `systemctl` 命令。`systemctl` 是 Linux 发行版新版本中提供的一个新命令,用于控制系统和服务。我们将探讨一些在 Linux 中使用 `systemctl` 命令的可能方法。
检查服务状态
我们可以使用此命令验证服务的状态。以下是检查服务的命令:
# systemctl status httpd httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2016-06-02 06:48:58 EDT; 27s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 10011 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─10011 /usr/sbin/httpd -DFOREGROUND ├─10012 /usr/sbin/httpd -DFOREGROUND ├─10013 /usr/sbin/httpd -DFOREGROUND ├─10014 /usr/sbin/httpd -DFOREGROUND ├─10015 /usr/sbin/httpd -DFOREGROUND └─10016 /usr/sbin/httpd -DFOREGROUND Jun 02 06:48:57 centos-linux-1.shared systemd[1]: Starting The Apache HTTP Server... Jun 02 06:48:58 centos-linux-1.shared systemd[1]: Started The Apache HTTP Server.
显示服务
# systemctl show httpd Type=notify Restart=no NotifyAccess=main RestartUSec=100ms TimeoutStartUSec=1min 30s TimeoutStopUSec=1min 30s WatchdogUSec=0 WatchdogTimestamp=Thu 2016-06-02 06:48:58 EDT WatchdogTimestampMonotonic=2588490928 StartLimitInterval=10000000StartLimitBurst=5 StartLimitAction=none …. . . . . AssertResult=yes ConditionTimestamp=Thu 2016-06-02 06:48:57 EDT ConditionTimestampMonotonic=2587709137 AssertTimestamp=Thu 2016-06-02 06:48:57 ED AssertTimestampMonotonic=2587709138 Transient=no This will display all the status information about http (web servers services) Verifying the Services active or enabled Here we will verify that the servers are active and they are enabled at system startup To verify the services active or not we needed to run the below command # systemctl is-active httpd.service active
启用服务在系统启动时或机器重启后运行
# systemctl enable httpd Created symlink from /etc/systemd/system/multi-use.rtarget.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
要检查服务是否已启用,请运行以下命令:
# systemctl is-enabled httpd enabled
要手动启动或重启服务,我们可以使用以下命令:
# systemctl start httpd # systemctl restart httpd
要列出依赖项,我们可以使用以下命令:
# systemctl list-dependencies httpd httpd.service ├─-.mount ├─system.slice └─basic.target ├─microcode.service ├─rhel-autorelabel-mark.service ├─rhel-autorelabel. service ├─rhel-configure.service ├─rhel-dmesg.service ├─rhel-loadmodules.service ├─paths.target ├─slices.target │ ├─-.slice │ └─system.slice… … . ..
列出当前在后台运行的所有作业
# systemctl list-jobs No jobs running.
列出所有已安装的单元文件
# systemctl list-unit files
列出特定服务的全部已安装单元文件
# systemctl list-unit-files | grep httpd httpd.service enabled
列出所有已安装的套接字及其类型
# systemctl list-sockets --show-types LISTEN TYPE UNIT ACTIVATES /dev/log Datagram systemd-journald.socket systemd-j /run/dmeventd-client FIFO dm-event.socket dm-event. /run/dmeventd-server FIFO dm-event.socket dm-event. /run/lvm/lvmetad.socket Stream lvm2-lvmetad.socket lvm2-lvme /run/lvm/lvmpolld.socket Stream lvm2-lvmpolld.socket lvm2-lvmp /run/systemd/initctl/fifo FIFO systemd-initctl.socket systemd-i /run/systemd/journal/socket Datagram systemd-journald.socket systemd-j /run/systemd/journal/stdout Stream systemd-journald.socket systemd-j /run/systemd/shutdownd Datagram systemd-shutdownd.socket systemd-s /run/udev/control SequentialPacket systemd-udevd-control.socket systemd-u /var/run/dbus/system_bus_socket Stream dbus.socket dbus.serv kobject-uevent 1 Netlink systemd-udevd-kernel.socket systemd-u 12 sockets listed. Pass --all to see loaded but inactive sockets, too.
设置/列出默认目标(即运行级别)
# systemctl set-default multi-user.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target. # systemctl get-default multi-user.target
屏蔽和取消屏蔽服务
在某些情况下,尤其是在大型公司中,一位管理员可能会停止服务器,而另一位管理员则会启动服务。为了避免这种情况,我们可以屏蔽和取消屏蔽服务。如果某些服务在未取消屏蔽的情况下被停止,我们将无法再次启动这些服务。
# systemctl mask httpd Created symlink from /etc/systemd/system/httpd.service to /dev/null.
查看屏蔽服务后的状态
# httpd.service Loaded: masked (/dev/null) Active: active (running) since Thu 2016-06-02 07:03:27 EDT; 10min ago Main PID: 10128 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─10128 /usr/sbin/httpd -DFOREGROUND ├─10129 /usr/sbin/httpd -DFOREGROUND ├─10130 /usr/sbin/httpd -DFOREGROUND ├─10131 /usr/sbin/httpd -DFOREGROUND ├─10132 /usr/sbin/httpd -DFOREGROUND └─10133 /usr/sbin/httpd -DFOREGROUND Jun 02 07:03:27 centos-linux-1.shared systemd[1]: Starting The Apache HTTP Server... Jun 02 07:03:27 centos-linux-1.shared systemd[1]: Started The Apache HTTP Server. Jun 02 07:13:47 centos-linux-1.shared systemd[1]: httpd.service: Got notification m.... Jun 02 07:13:57 centos-linux-1.shared systemd[1]: httpd.service: Got notification m.... Jun 02 07:14:07 centos-linux-1.shared systemd[1]: httpd.service: Got notification m.... Jun 02 07:14:17 centos-linux-1.shared systemd[1]: httpd.service: Got notification m.... Hint: Some lines were ellipsized, use -l to show in full.
如果我们尝试启动被屏蔽的服务,将会显示错误。
# systemctl start httpd Failed to start httpd.service: Unit httpd.service is masked.
要取消屏蔽服务以启动,我们需要运行以下命令:
# systemctl unmask httpd Removed symlink /etc/systemd/system/httpd.service.
取消屏蔽服务后,我们可以再次启动服务。
# systemctl start httpd # systemctl status httpd httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2016-06-02 07:03:27 EDT; 18min ago Docs: man:httpd(8) man:apachectl(8) Main PID: 10128 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─10128 /usr/sbin/httpd -DFOREGROUND ├─10129 /usr/sbin/httpd -DFOREGROUND ├─10130 /usr/sbin/httpd -DFOREGROUND ├─10131 /usr/sbin/httpd -DFOREGROUND ├─10132 /usr/sbin/httpd -DFOREGROUND └─10133 /usr/sbin/httpd -DFOREGROUND
我们可以创建 Linux 机器上服务的快照。
快照是指系统管理器的已保存状态。快照仅用于保存和恢复正在运行或已停止的单元。
要创建服务的快照,我们可以使用:
# systemctl snapshot httpd ht.tpdsnapshot
查看服务快照的状态
# systemctl status httpd.snapshot httpd.snapshot Loaded: loaded Active: inactive (dead) Jun 02 07:26:31 centos-linux-1.shared systemd[1]: Created snapshot httpd.snapshot.
删除快照并检查服务快照的状态
# systemctl delete httpd.snapshot # systemctl status httpd.snapshot httpd.snapshot Loaded: not-found (Reason: No such file or directory) Active: inactive (dead) Jun 02 07:26:31 centos-linux-1.shared systemd[1]: Created snapshot httpd.snapshot. Jun 02 07:29:05 centos-linux-1.shared systemd[1]: Removing snapshot httpd.snapshot.
通过本文,我们现在能够控制 Linux 新版本或最新版本中的服务,其中 `systemctl` 命令取代了旧版 Linux 中的 `service` 命令。希望这将有助于您的工作。
广告