Operation and maintenance
  • 目录
  • linux 历史
    • Linux网卡配置
    • linux互相登陆
  • 文件目录管理
    • linux目录结构
    • 文件的基本信息
    • linux别名alias
    • 环境变量
    • linux终端相关快捷键
    • linux文件和目录添加删除
    • linux文件查看命令
    • linux文件目录权限
    • linux文件特殊权限
    • 链接文件
    • linux下文件查找
    • linux和window文件互传
    • linux文件压缩和打包
    • vim工具集
  • 用户和组管理
    • 用户组和用户管理
    • 修改组和用户
    • 用户切换
    • 禁止root远程登陆
  • linux磁盘管理
    • 磁盘分区
    • 格式化磁盘
    • 分区挂载
    • 手动添加swap分区
    • lvm基础
  • linux软件安装和卸载
    • rpm工具
    • yum工具
    • 源码安装包
  • 日常运维
    • 监控磁盘IO
    • 查看网络状态
    • linux抓包
    • linux网络相关
    • Linux防火墙-netfilter
    • linux任务计划
    • linux系统服务管理
    • linux同步工具rsync
    • linux系统日志和screen
  • LAMP架构
    • 安装mysql
    • 免编译安装mariadb
    • 安装apache
    • 安装php5
    • 安装php7
    • apache 配置虚拟主机
    • apache 用户认证
    • apache 域名跳转
    • apache 访问日志
    • apache 不记录文件日志
    • apache 配置静态元素过期时间
    • apache 配置防盗链
    • apache 访问控制
    • apache 禁止解析php文件
    • apache 禁止user_agent
    • php 配置文件
    • php 动态扩展模块安装
  • LNMP架构
    • 安装mariadb
    • 安装php
    • 安装nginx
    • nginx 默认虚拟主机
    • nginx 用户认证
    • nginx 域名重定向
    • nginx 访问日志
    • nginx 日志切割
    • nginx不记录指定文件类型日志
    • nginx 静态文件和过期时间
    • nginx 防盗链
    • nginx 访问控制
    • nginx 解析php
    • nginx 代理
    • nginx 负载均衡
    • nginx 支持https
  • MySQL集合
    • 更改root密码
    • mysql 常用操作
    • mysql 常用sql
    • mysql 数据备份
    • mysql 主从配置
  • linux常用服务
    • nfs 服务搭建
    • CentOS7 搭建snmp服务
    • ftp 服务搭建
    • samba 服务搭建
    • Squid 服务搭建
    • Tomcat 服务搭建
    • resin 搭建
    • dns 服务搭建
    • iscsi 服务搭建
  • linux开发语言环境
    • python 开发环境
    • golang 开发环境
  • linux集群架构
    • linux 高可用
    • linux 负载均衡
  • linux监控平台
    • zabbix 监控平台(上)
  • shell集合
  • NoSql
    • memcached (上)
    • memcached(下)
    • redis(上)
    • redis(中)
    • redis(下)
    • mongodb(上)
    • mongodb(中)
    • mongodb(下)
  • 代码管理工具
    • 代码管理工具 svn
    • 代码管理工具 git
    • 搭建git私有仓库
    • 安装配置gitlab
  • docker 入门
    • docker 安装
    • docker 镜像管理
    • docker 容器管理
    • docker 仓库管理
    • docker 共享目录
    • docker 数据备份和恢复
    • docker 网络管理
    • docker 使用dockerfile
    • docker compose
  • 自动化运维
    • saltstack 安装
    • saltstack 配置认证
    • saltstack 远程执行命令
    • saltstack grains
    • saltstack pillar
    • saltstack 实战:安装配置httpd
    • saltstack 实战:配置管理文件
    • saltstack 实战:配置管理远程命令
    • saltstack 实战:配置管理任务计划
    • saltstack salt-ssh工具
    • ansible 安装
    • ansible 远程命令
    • ansible 远程目录拷贝
    • ansible 任务计划
    • ansible 远程rpm/服务管理
    • ansible playbook 详解
    • ansible playbook 实战:安装nginx
  • linux虚拟化
    • KVM介绍
    • 安装KVM
    • 虚拟机管理
    • 克隆虚拟机
    • 快照管理
    • 磁盘扩容
    • 调整虚拟机配置
    • 迁移虚拟机
  • 错误排查
    • linux启动挂在不上sysroot问题
由 GitBook 提供支持
在本页
  • linux系统服务管理
  • 管理工具 - chkconfig
  • 管理工具 - systemd

这有帮助吗?

  1. 日常运维

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/

[root@localhost ~]# ls /etc/init.d/
functions  netconsole  network  README

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

[root@localhost ~]# chkconfig --level 2345 network off # network 2345 级别关闭
[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:关
[root@localhost ~]# chkconfig --level 2345 network on # network 2345 级别启动
[root@localhost ~]# chkconfig --del network  # 删除服务(注:启动脚本要再/etc/init.d/ 下)
[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:关
[root@localhost ~]# chkconfig --add network # 添加服务(注:启动脚本要再/etc/init.d/ 下)
[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: 2345 10 90 #(必须:运行级别 开机开启顺序,越小越早,关机关闭顺序)
. /etc/init.d/functions #(必须)

chkconfig 设置开启启动实例:nginx

[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}

reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}

restart(){
stop
start
}

configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}

case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac

exit $RETVAL
[root@localhost ~]# chmod 755 /etc/init.d/nginx
[root@localhost ~]# chkconfig --add 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 //检查服务是否开机启动

[root@localhost ~]# systemctl disable network
network.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig network off
[root@localhost ~]# systemctl enable network
network.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig network on
[root@localhost ~]# systemctl restart network
[root@localhost ~]# systemctl stop network
[root@localhost ~]# systemctl start network
[root@localhost ~]# systemctl is-enabled network
network.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig network --level=5
enabled

systemd 中的系统运行级别:

[root@localhost ~]# ls -l /usr/lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 15 7月  12 10:32 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 7月  12 10:32 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 7月  12 10:32 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 7月  12 10:32 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 7月  12 10:32 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 7月  12 10:32 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 7月  12 10:32 /usr/lib/systemd/system/runlevel6.target -> reboot.target

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

[root@localhost ~]# vim /etc/systemd/system/nginx
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

解析:

[Unit]

Description : 服务的简单描述

Documentation : 服务文档

Before、After:定义启动顺序。Before=xxx.service,代表本服务在xxx.service启动之前启动。After=xxx.service,代表本服务在xxx.service之后启动。

Requires:这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,这个单元也停止了。

Wants:推荐使用。这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,对本单元没有影响。

[Service]

Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。

Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。

Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。

Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。

Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。

Type=idle: systemd会等待所有任务(Jobs)处理完成后,才开始执行idle类型的单元。除此之外,其他行为和Type=simple 类似。

PIDFile:pid文件路径

ExecStart:指定启动单元的命令或者脚本,ExecStartPre和ExecStartPost节指定在ExecStart之前或者之后用户自定义执行的脚本。Type=oneshot允许指定多个希望顺序执行的用户自定义命令。

ExecReload:指定单元停止时执行的命令或者脚本。

ExecStop:指定单元停止时执行的命令或者脚本。

PrivateTmp:True表示给服务分配独立的临时空间

Restart:这个选项如果被允许,服务重启的时候进程会退出,会通过systemctl命令执行清除并重启的操作。

RemainAfterExit:如果设置这个选择为真,服务会被认为是在激活状态,即使所以的进程已经退出,默认的值为假,这个选项只有在Type=oneshot时需要被配置。

[Install]

Alias:为单元提供一个空间分离的附加名字。

RequiredBy:单元被允许运行需要的一系列依赖单元,RequiredBy列表从Require获得依赖信息。

WantBy:单元被允许运行需要的弱依赖性单元,Wantby从Want列表获得依赖信息。

Also:指出和单元一起安装或者被协助的单元。

DefaultInstance:实例单元的限制,这个选项指定如果单元被允许运行默认的实例。
上一页linux任务计划下一页linux同步工具rsync

最后更新于4年前

这有帮助吗?