1. 程式人生 > >RHEL 7中systemctl用法(替換service和chkconfig)

RHEL 7中systemctl用法(替換service和chkconfig)

systemctl用法 centos 7服務

傳統的Linux系統啟動過程主要由著名的init進程(也被稱為SysV init啟動系統)處理,而基於init的啟動系統被認為有效率不足的問題,systemdLinux系統機器的另一種啟動方式,宣稱彌補了以傳統Linux SysV init為基礎的系統的缺點。


1、列出正在運行的服務

[root@localhost ~]# systemctl 
  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:07.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged   VMware_Virtual_IDE_CDROM_Drive CentOS_7_x86_64
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged   VMware_Virtual_S 1
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plugged   LVM PV CSeHGs-hdrA-6Y0N-jXVM-VA3R-3sSe-BraSga on /dev/sda2 2
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged   VMware_Virtual_S
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb.device loaded active plugged   VMware_Virtual_S
  sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device              loaded active plugged   82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Single Port Adapter)
  sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device            loaded active plugged   ES1371/ES1373 / Creative Labs CT2518 (Audio PCI 64V/128/5200 / Creative CT4810/
  sys-devices-platform-serial8250-tty-ttyS1.device                               loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS1
  sys-devices-platform-serial8250-tty-ttyS2.device                               loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS2
  sys-devices-platform-serial8250-tty-ttyS3.device                               loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS3
  sys-devices-pnp0-00:05-tty-ttyS0.device                                        loaded active plugged   /sys/devices/pnp0/00:05/tty/ttyS0
  sys-devices-virtual-block-dm\x2d0.device                                       loaded active plugged   /sys/devices/virtual/block/dm-0
  sys-devices-virtual-block-dm\x2d1.device                                       loaded active plugged   /sys/devices/virtual/block/dm-1
  sys-module-configfs.device                                                     loaded active plugged   /sys/module/configfs
  sys-subsystem-net-devices-ens33.device                                         loaded active plugged   82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Single Port Adapter)
  -.mount                                                                        loaded active mounted   /
  boot.mount                                                                     loaded active mounted   /boot



2、樹狀結構顯示正在運行的服務

[root@localhost ~]# systemd-cgls
├─1 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
├─user.slice
│ └─user-0.slice
│   └─session-1.scope
│     ├─1649 sshd: root@pts/0 
│     ├─1967 -bash
│     ├─2860 systemd-cgls
│     └─2861 systemd-cgls
└─system.slice
  ├─rsyslog.service
  │ └─1554 /usr/sbin/rsyslogd -n
  ├─umail_postgresql.service
  │ ├─1589 /usr/local/u-mail/service/pgsql-9.4/bin/postgres -D /usr/local/u-mail/service/pgsql-9.4/data/data
  │ ├─1672 postgres: logger process                                                                         
  │ ├─1719 postgres: checkpointer process                                                                   
  │ ├─1720 postgres: writer process                                                                         
  │ ├─1721 postgres: wal writer process                                                                     
  │ ├─1724 postgres: autovacuum launcher process                                                            
  │ └─1726 postgres: stats collector process                                                                
  ├─tuned.service
  │ └─1546 /usr/bin/python -Es /usr/sbin/tuned -l -P


3、systemctl常見操作命令

啟動服務:systemctl start postfix.service
關閉服務:systemctl stop postfix.service
重啟服務:systemctl restart postfix.service
查看服務狀態:systemctl status postfix.service
開機啟動服務:systemctl enable postfix.service
開機禁用服務:systemctl disable postfix.service
確認開機是否啟動服務:systemctl is-enabled postfix.service
查看已啟動的服務:systemctl list-unit-files|grep enabled

查看啟動失敗的服務:systemctl --failed
使用命令 systemctl is-enabled postfix.service返回的結果有enable、disable或static。static它是指對應的 Unit 文件中沒有定義[Install]區域,因此無法配置為開機啟動服務。

說明:啟用服務是在當前“runlevel”的配置文件目錄/etc/systemd/system/multi-user.target.wants/裏,建立/usr/lib/systemd/system裏面對應服務配置文件的軟鏈接;禁用服務就是刪除此軟鏈接,添加服務就是添加軟連接。



RHEL 7中systemctl用法(替換service和chkconfig)