天使羊波波闪耀光芒 软件及互联网爱好者

2611月/170

CentOS7 systemctl命令初探

CentOS7 systemctl命令初探

CentOS7在系统结构上和CentOS5、6有了很大的区别,它用systemd代替了init作为用户的初始进程。systemctl命令是CentOS7下的管理命令。

1、用户启动进程类别
Linux系统主要有以下几种主流选择:
(1)以Ubuntu为代表的Linux发行版采用upstart。
(2)以7.0版本之前的CentOS为代表的System V init。
(3)CentOS7.0版本的systemd。

2、systemctl命令

[root@server201 ~]# systemctl
  UNIT                                    LOAD   ACTIVE SUB       DESCRIPTION
  sys-devices-virtual-net-venet0.device   loaded active plugged   /sys/devices/virtual/net/venet0
  sys-subsystem-net-devices-venet0.device loaded active plugged   /sys/subsystem/net/devices/venet0
  -.mount                                 loaded active mounted   /
  dev-mqueue.mount                        loaded active mounted   POSIX Message Queue File System
  run-user-0.mount                        loaded active mounted   /run/user/0
  brandbot.path                           loaded active waiting   Flexible branding
  systemd-ask-password-console.path       loaded active waiting   Dispatch Password Requests to Console Directory Watch
  systemd-ask-password-wall.path          loaded active waiting   Forward Password Requests to Wall Directory Watch
  session-29566954.scope                  loaded active running   Session 29566954 of user root
  console-getty.service                   loaded active running   Console Getty
  crond.service                           loaded active running   Command Scheduler
  dbus.service                            loaded active running   D-Bus System Message Bus
  firewalld.service                       loaded active running   firewalld - dynamic firewall daemon
  getty@tty2.service                      loaded active running   Getty on tty2
  httpd.service                           loaded active running   The Apache HTTP Server
  network.service                         loaded active exited    LSB: Bring up/down networking
  quotaon.service                         loaded active exited    Enable File System Quotas
  rhel-dmesg.service                      loaded active exited    Dump dmesg to /var/log/dmesg
  rhel-readonly.service                   loaded active exited    Configure read-only root support
  rsyslog.service                         loaded active running   System Logging Service
  saslauthd.service                       loaded active running   SASL authentication daemon.
  sendmail.service                        loaded active running   Sendmail Mail Transport Agent
  sm-client.service                       loaded active running   Sendmail Mail Transport Client
  sshd.service                            loaded active running   OpenSSH server daemon
  systemd-journal-flush.service           loaded active exited    Flush Journal to Persistent Storage
  systemd-journald.service                loaded active running   Journal Service
  systemd-logind.service                  loaded active running   Login Service
  systemd-random-seed.service             loaded active exited    Load/Save Random Seed
  systemd-remount-fs.service              loaded active exited    Remount Root and Kernel File Systems
* systemd-sysctl.service                  loaded failed failed    Apply Kernel Variables
  systemd-tmpfiles-setup.service          loaded active exited    Create Volatile Files and Directories
  systemd-udev-trigger.service            loaded active exited    udev Coldplug all Devices
  systemd-udevd.service                   loaded active running   udev Kernel Device Manager
  systemd-update-utmp.service             loaded active exited    Update UTMP about System Boot/Shutdown
  systemd-user-sessions.service           loaded active exited    Permit User Sessions
* systemd-vconsole-setup.service          loaded failed failed    Setup Virtual Console
  xinetd.service                          loaded active running   Xinetd A Powerful Replacement For Inetd
  xinetd.service                          loaded active running   Xinetd A Powerful Replacement For Inetd
  -.slice                                 loaded active active    Root Slice
  system-getty.slice                      loaded active active    system-getty.slice
  system.slice                            loaded active active    System Slice
  user-0.slice                            loaded active active    User Slice of root
  user.slice                              loaded active active    User and Session Slice
  dbus.socket                             loaded active running   D-Bus System Message Bus Socket
  rpcbind.socket                          loaded active listening RPCbind Server Activation Socket
  systemd-initctl.socket                  loaded active listening /dev/initctl Compatibility Named Pipe
  systemd-journald.socket                 loaded active running   Journal Socket
  systemd-shutdownd.socket                loaded active listening Delayed Shutdown Socket
  systemd-udevd-control.socket            loaded active running   udev Control Socket
  systemd-udevd-kernel.socket             loaded active running   udev Kernel Socket
  basic.target                            loaded active active    Basic System
  cryptsetup.target                       loaded active active    Encrypted Volumes
  getty.target                            loaded active active    Login Prompts
  graphical.target                        loaded active active    Graphical Interface
  local-fs-pre.target                     loaded active active    Local File Systems (Pre)
  local-fs.target                         loaded active active    Local File Systems
  multi-user.target                       loaded active active    Multi-User System
  network-online.target                   loaded active active    Network is Online
  network-pre.target                      loaded active active    Network (Pre)
  network.target                          loaded active active    Network
  paths.target                            loaded active active    Paths
  remote-fs.target                        loaded active active    Remote File Systems
  slices.target                           loaded active active    Slices
  sockets.target                          loaded active active    Sockets
  swap.target                             loaded active active    Swap
  sysinit.target                          loaded active active    System Initialization
  timers.target                           loaded active active    Timers
  systemd-tmpfiles-clean.timer            loaded active waiting   Daily Cleanup of Temporary Directories

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

67 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.


可以看到systemctl命令显示分为五列:
UNIT:单元名称
LOAD:单元的加载状态
ACTIVE:单元的启动状态
SUB:单元的运行状态
DESCRIPTION:单元说明

3、Unit分类
unit有许多种后缀,分为不同的类型:
1).automount:用于控制自动挂载文件系统。自动挂载即当某一目录被访问时系统自动挂载该目录,这类unit取代了传统Linux系统的autofs相应功能
2).device:对应 /dev 目录下设备,主要用于定义设备之间的依赖关系
3).mount:定义系统结构层次中的一个挂载点,可以替代过去的/etc/fstab配置文件
4).path:用于监控指定目录变化,并触发其他unit运行
5).scope:这类unit文件不是用户创建的,而是Systemd运行时自己产生的,描述一些系统服务的分组信息
6).service:封装守护进程的启动、停止、重启和重载操作,是最常见的一种unit类型
7).slice:用于描述cgroup的一些信息,极少使用到,一般用户就忽略它吧
8).snapshot:这种unit其实是systemctl snapshot命令创建的一个描述Systemd unit运行状态的快照
9).socket:监控系统或互联网中的socket消息,用于实现基于网络数据自动触发服务启动
10).swap:定义一个用于做虚拟内存的交换分区
11).target:用于对unit进行逻辑分组,引导其他unit的执行。它替代了SysV中运行级别的作用,并提供更灵活的基于特定设备事件的启动方式。例如multi-user.target相当于过去的运行级别5,而bluetooth.target在有蓝牙设备接入时就会被触发
12).timer:封装由system的里面由时间触发的动作, 替代了crontab的功能

4、systemctl命令代替service、chkconfig
实现启动、停止、管理服务功能,以httpd服务为例:

1)设置服务自启动

systemctl enable httpd

2)关闭服务自启动

systemctl disable httpd

3)启动httpd服务

systemctl start httpd.service

4)查看当前httpd状态

[root@server201 ~]# systemctl status httpd.service
* httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2017-11-26 05:17:11 UTC; 46min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 602 (httpd)
   Status: "Total requests: 14; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           |-602 /usr/sbin/httpd -DFOREGROUND
           |-613 /usr/sbin/httpd -DFOREGROUND
           |-630 /usr/sbin/httpd -DFOREGROUND
           |-648 /usr/sbin/httpd -DFOREGROUND
           |-649 /usr/sbin/httpd -DFOREGROUND
           `-650 /usr/sbin/httpd -DFOREGROUND

Nov 26 05:11:50 server201 systemd[1]: Stopping The Apache HTTP Server...
Nov 26 05:11:51 server201 systemd[1]: Starting The Apache HTTP Server...
Nov 26 05:11:52 server201 httpd[1494]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 107.181.152.135. Set the 'ServerN...his message
Nov 26 05:11:52 server201 systemd[1]: Started The Apache HTTP Server.
Nov 26 05:16:54 server201 systemd[1]: Stopping The Apache HTTP Server...
Nov 26 05:16:55 server201 systemd[1]: Stopped The Apache HTTP Server.
Nov 26 05:17:10 server201 systemd[1]: Starting The Apache HTTP Server...
Nov 26 05:17:11 server201 httpd[602]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 107.181.152.135. Set the 'ServerN...this message
Nov 26 05:17:11 server201 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.


5)查看所有服务自启动状态

systemctl list-unit-files

参考资料:
http://blog.51cto.com/carllai/1439562
http://blog.csdn.net/smstong/article/details/39317491

评论 (0) 引用 (0)

还没有评论.


Leave a comment

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!:

还没有引用.