系统服务管理工具systemd

来自CloudWiki
跳转至: 导航搜索
  • 在CentOS版本中,一个很重要的改变就是使用systemd管理机制,它不仅可能完成系统的初始化工作,还能对系统服务进行管理。虽然切换到了systemd,但是systemd仍然兼容sysvinit和Linux标准组的启动脚本。
  • systemd在系统中是一个用户级的应用程序,它包含一个完整的软件包,配置文件位于/etc/systemd这个目录下。配置工具命令位于/bin、/sbin这两个目录下,备用配置文件位于/lib/systemd目录下。可通过命令"rpm -ql systemd"查看所有文件的安装路径。
  • systemd提供了一个非常强大的命令行工具systemctl,要查看启动,停止,重启,启用或者禁用系统服务,都可以通过systemd命令来实现。

启动、停止、重启服务

  • 要通过systemd命令启动一个服务,可以使用如下命令。
 systemctl start httpd.service
  • 例如,我们要启动httpd服务,也就是Apache HTTP服务器,就可以使用这条命令
yum -y install httpd
systemctl start httpd.service
  • 要停止它就可以使用这条命令。
systemctl stop httpd.service
  • 要重启httpd服务,可以使用restart选项,此选项的含义是,如果服务在运行中,它将重启服务,如果服务不在运行中,它将会启动。

查看、禁止、启用服务

  • 可以查看一个服务的运行状态。
[root@mycloud ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Active: active (running) since Sun 2018-02-04 11:57:03 EST; 2min 52s ago
    Docs: man:httpd(8)
          man:apachectl(8)
Main PID: 20876 (httpd)
  Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
  CGroup: /system.slice/httpd.service
          ├─20876 /usr/sbin/httpd -DFOREGROUND
          ├─20877 /usr/sbin/httpd -DFOREGROUND
          ├─20878 /usr/sbin/httpd -DFOREGROUND
          ├─20879 /usr/sbin/httpd -DFOREGROUND
          ├─20880 /usr/sbin/httpd -DFOREGROUND
          └─20881 /usr/sbin/httpd -DFOREGROUND
Feb 04 11:57:03 mycloud systemd[1]: Starting The Apache HTTP Server...
Feb 04 11:57:03 mycloud httpd[20876]: AH00558: httpd: Could not reliably determine the server's fully qualified domain  name, using 123.129.254.12. Set the 'ServerName' directi... this message
Feb 04 11:57:03 mycloud systemd[1]: Started The Apache HTTP Server.

Hint: Some lines were ellipsized, use -l to show in full.

  • 从图中可以看出,该服务处于运行状态,并且还能看到服务的启动时间,及服务对应的每个进程的PID信息。
  • 通过systemctl命令可以启用禁用服务,以此来控制开机服务启动,可以使用选项enable/disable来控制一个服务是否开机启动。
[root@mycloud ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/systemhttpd.service.
[root@mycloud ~]# systemctl disable httpd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.

systemd电源管理命令

  • systemd命令还能干:
systemdctl poweroff:关闭系统
systemdctl reboot:重启系统
systemdctl suspend:进入待机模式
systemdctl hibernate:进入休眠模式
systemdctl hybrid-sleep:进入混合休眠模式