10.25 systemd管理服务

时间:2022-04-27
本文章向大家介绍10.25 systemd管理服务,主要内容包括Linux系统服务管理-systemd、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 ~]#