linux系统服务管理

linux系统服务管理

CentOS 中有一些的工具可以使系统的服务开机自动启动和关闭。

管理工具 - chkconfig

CentOS6中使用,CentOS7保留过度的。

[root@localhost ~]# 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:关

chkconfig 配置的启动脚本路径/etc/init.d/

chkconfig的使用:

用法:

  • chkconfig [--list] [--type <type>] [name]

    :列出所有服务

  • chkconfig --add <name>

    :添加开机启动服务

  • chkconfig --del <name>

    :删除开启启动服务

  • chkconfig --override <name>

    :服务概览

  • chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities> :设置启动级别

CentOS6即以下的linux发行版的服务运行级别:

  • 0 关机

  • 1 单用户,不用密码即可登录,修改root密码

  • 2 多用户级别,但没有NFS

  • 3 完整多用户级别

  • 4 保留

  • 5 图形界面级别

  • 6 重启级别 init6

启动脚本的格式:

chkconfig 设置开启启动实例:nginx

管理工具 - systemd

在CentOS7及以后的版本中代替chkconfig。

用法:

  • 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 中的系统运行级别:

ls /usr/lib/systemd/system //系统所有unit,分为以下类型

  • service 系统服务

  • target 多个unit组成的组

  • device 硬件设备

  • mount 文件系统挂载点

  • automount 自动挂载点

  • path 文件或路径

  • scope 不是由systemd启动的外部进程

  • slice 进程组

  • snapshot systemd快照

  • socket 进程间通信套接字

  • swap swap文件

  • timer 定时器

unit相关的命令

  • systemctl list-units //列出正在运行的

  • unit systemctl list-units --all //列出所有,包括失败的或者inactive的

  • systemctl list-units --all --state=inactive //列出inactive的unit

  • systemctl list-units --type=service//列出状态为active的service

  • systemctl is-active crond.service //查看某个服务是否为active

系统为了方便管理用target来管理unit

  • systemctl list-unit-files --type=target

  • systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit

  • systemctl get-default //查看系统默认的target

  • systemctl set-default multi-user.target

    一个service属于一种类型的unit ,多个unit组成了一个target 一个target里面包含了多个service

cat /usr/lib/systemd/system/sshd.service //看[install]部分

systemd设置开启启动实例:nginx

解析:

最后更新于

这有帮助吗?