前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >10.25 systemd管理服务

10.25 systemd管理服务

作者头像
运维小白
发布2018-02-06 15:01:08
5930
发布2018-02-06 15:01:08
举报
文章被收录于专栏:运维小白

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 //查看所有的服务
代码语言:javascript
复制
[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
代码语言:javascript
复制
[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 //不让开机启动
代码语言:javascript
复制
[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 //查看状态
代码语言:javascript
复制
[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 //检查服务是否开机启动
代码语言:javascript
复制
[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的配置文件内容
代码语言:javascript
复制
[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 ~]# 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Linux系统服务管理-systemd
    • systemd工具
      • systemd相关的命令
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档