10.25 systemd管理服務

Linux系統服務管理-systemd

  • systemctl list-units --all --type=service
  • 幾個經常使用的服務相關的命令
  • systemctl enable crond.service //讓服務開機啓動
  • systemctl disable crond //不讓開機啓動
  • systemctl status crond //查看狀態
  • systemctl stop crond //中止服務
  • systemctl start crond //啓動服務
  • systemctl restart crond //重啓服務
  • systemctl is-enabled crond //檢查服務是否開機啓動

systemd工具

  • systemd是centos7管理的一個服務機制,在centos6或以前的版本中能夠使用chkconfig工具去管理系統的服務,在centos7中,也能夠使用,但會提示使用 systemctl list-unit-files ,用它來查看全部的服務。
  • systemctl list-unit-files //查看全部的服務
[root@hf-01 init.d]# chkconfig --list

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      若是您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole     	0:關	1:關	2:關	3:關	4:關	5:關	6:關
network        	0:關	1:關	2:開	3:開	4:開	5:開	6:關
[root@hf-01 init.d]# systemctl list-unit-files    //查看全部的服務,裏面不只有service,還有socket,還有target
UNIT FILE                                   STATE   
proc-sys-fs-binfmt_misc.automount           static  
dev-hugepages.mount                         static  
dev-mqueue.mount                            static  
proc-sys-fs-binfmt_misc.mount               static  
sys-fs-fuse-connections.mount               static  
sys-kernel-config.mount                     static  
sys-kernel-debug.mount                      static  
tmp.mount                                   disabled
brandbot.path                               disabled
等等

systemd相關的命令

  • systemctl list-units --all --type=service //列出全部的service
    • 會列出全部的service
    • 列出描述信息,是不是loaded,是不是active
    • 按 空格 往下翻
    • 如果不加 --all ,則就不會列出 未激活的active
[root@hf-01 ~]# systemctl list-units --all --type=service    //列出全部的service
  UNIT                           LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service                 loaded    active   running Security Auditing Service
  avahi-daemon.service           loaded    active   running Avahi mDNS/DNS-SD Stack
  brandbot.service               loaded    inactive dead    Flexible Branding Service
  cpupower.service               loaded    inactive dead    Configure CPU power related 
  crond.service                  loaded    active   running Command Scheduler

等等等,只截取了一部分
並在最下面,會告訴你 LOAD,ACTIVE,SUB是什麼意思
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.

還會提醒,若想列出全部的 unit files,請使用 systemctl list-unit-files 命令
  • systemctl enable crond.service //讓服務開機啓動——>service可省略
  • systemctl disable crond //不讓開機啓動
[root@hf-01 ~]# systemctl enable crond.service        //讓服務開機啓動
[root@hf-01 ~]# systemctl disable crond.service        //不讓開機啓動
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@hf-01 ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@hf-01 ~]#
  • systemctl status crond //查看狀態
[root@hf-01 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 二 2017-12-05 01:37:49 CST; 5h 15min ago
 Main PID: 574 (crond)
   CGroup: /system.slice/crond.service
           └─574 /usr/sbin/crond -n

12月 05 01:37:49 hf-01 systemd[1]: Started Command Scheduler.
12月 05 01:37:49 hf-01 systemd[1]: Starting Command Scheduler...
12月 05 01:37:50 hf-01 crond[574]: (CRON) INFO (RANDOM_DELAY will be scaled with ...d.)
12月 05 01:37:50 hf-01 crond[574]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.
[root@hf-01 ~]#
  • systemctl stop crond //中止服務
  • systemctl start crond //啓動服務
  • systemctl restart crond //重啓服務
  • systemctl is-enabled crond //檢查服務是否開機啓動
[root@hf-01 ~]# systemctl is-enabled crond
enabled
[root@hf-01 ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@hf-01 ~]# systemctl is-enabled crond
disabled
[root@hf-01 ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@hf-01 ~]#
  • 而且能夠經過輸出信息,在 /etc/systemd/system/multi-user.target.wants/crond.service 得到service的配置文件內容
[root@hf-01 ~]# cat /etc/systemd/system/multi-user.target.wants/crond.service    得到service的配置文件內容
[Unit]
Description=Command Scheduler
After=syslog.target auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
KillMode=process

[Install]
WantedBy=multi-user.target

[root@hf-01 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service        //是一個軟鏈接,從軟連接的右邊到左邊
lrwxrwxrwx 1 root root 37 12月  5 06:55 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
[root@hf-01 ~]# ls -l /usr/lib/systemd/system/crond.service    //這裏纔是文件真正的路徑
-rw-r--r--. 1 root root 263 6月  10 2014 /usr/lib/systemd/system/crond.service
[root@hf-01 ~]#
相關文章
相關標籤/搜索