本主题将从3个角度进行对比

  1. 常见设置(CentOS 6 vs CentOS 7)
  2. 服务管理(Sysvinit vs Upstart vs Systemd)
  3. 性能测试(cpu/mem/io/oltp)

本文为第二部分:服务管理的对比


1. sysvinit、upstart、systemd简介

/ CentOS 5 CentOS 6 CentOS 7 备注
sysvinit 第一代,传统,兼容最多(/etc/init.d/、/etc/rc.X)
upstart 第二代,形似systemd雏形(/etc/init)
systemd 第三代,配合cgroup,systemd完全接管整个系统(/usr/lib/systemd)

2. sysvinit、upstart、systemd常用命令

动作 sysvinit upstart systemd
查看 service mytest status initctl status mytest systemctl status mytest.service
启动 service mytest start initctl start mytest systemctl start mytest.service
关闭 service mytest stop initctl stop mytest systemctl stop mytest.service
强杀进程 kill -9 PID kill -9 PID systemctl kill mytest.service --signal=9
重启 service mytest restart initctl restart mytest systemctl restart mytest.service
重载 service mytest reload initctl reload mytest systemctl reload mytest.service
开机启动 chkconfig mytest on /etc/init/mytest.conf里配置start on runlevel [3] systemctl enable mytest.service

3. runlevel运行级别

运行级别 CentOS 6 CentOS 7
0 halt runlevel0.target -> poweroff.target
1 Single user mode runlevel1.target -> rescue.target
2 Multiuser, without NFS runlevel2.target -> multi-user.target
3 Full multiuser mode runlevel3.target -> multi-user.target
4 unused runlevel4.target -> multi-user.target
5 X11 runlevel5.target -> graphical.target
6 reboot runlevel6.target -> reboot.target
查看 cat /etc/inittab systemctl get-default
开机生效 编辑/etc/inittab systemctl set-default multi-user.target
立即切换 init 5 systemctl isolate graphical.target

4. 日志查询

CentOS 6: 手工在/var/log/messages、/var/log/dmesg、/var/log/secure中grep,麻烦且效率低

CentOS 7: 统一使用journalctl,可以使用多个因素匹配,比如时间段、服务名、日志级别等等。另外,systemd日志默认经过压缩,是二进制文件,无法直接查看

journalctl常用命令 作用 CentOS 6比
journalctl 所有日志,包含系统、内核等等 手动在对应日志文件中grep
journalctl --dmesg 查看当前开机后的内核日志 dmesg
journalctl --boot 查看当前开机后的日志 先查当前开机启动时间,然后cat /var/log/...
journalctl --boot=-1 查看上一次启动的日志 查询上次开机到当前开机之间时间,然后cat /var/log/...
journalctl --since="2018-08-01 12:00:00" 查看从指定时间开始到当前的日志 手动在日志里grep
journalctl --since=yesterday --until=today 查看昨天0-24点的日志 手动在日志里grep
journalctl -n 20 查看最后10行 tail -n 20
journalctl -f 实时滚动显示最新日志 tail -f
journalctl -e 直接翻到最后 tail
journalctl -u mytest.service 查看指定服务日志 先查询日志保存路径,然后再cat查看
journalctl -p 0 查看指定日志级别的日志,日志级别从0到7 通过syslog将不同级别的日志放到不同文件中
journalctl -u mytest.service -o json-pretty或-o verbose 查看每条日志详细信息(包含元信息)
journalctl --disk-usage 查看日志所在的磁盘空间 du -shx /var/log/messages等

5. 实现守护进程

CentOS 6

  • sysvinit需要自行实现

    • nohup &
    • screen
    • supervisor
  • upstart和systemd类似,将程序运行在前台即可

CentOS 7

  • 由systemd启动,将程序运行在前台即可

6. sysvinit、upstart、systemd例子

sysvinit

cat > /etc/init.d/mytest <<EOF
. /etc/rc.d/init.d/functions start() { … }
stop() { … }
restart() { … }
reload() { … }
status() { … } case "$1" in
start)
start
;;
stop)
stop
;;

esac
exit $RETVAL
EOF chmod +x /etc/init.d/mytest
service mytest start

upstart

cat > /etc/init/mytest.conf <<EOF
start on runlevel [3]
description “mytest"
exec /root/mytest.sh
EOF initctl start mytest

systemd

cat > /usr/lib/systemd/system/mytest.service <<EOF
[Unit]
Description=mytest [Service]
Type=simple
ExecStart=/root/mytest.sh [Install]
WantedBy=multi-user.target
EOF systemctl start mytest

7. PID管理

  • sysvinit: 需要生成PID文件,用于后期关闭、重启等使用
  • upstart: 无需PID文件,upstart会记录主进程ID,子进程ID没有记录
  • systemd: 无需PID文件,所有进程ID由cgroup统一接管

8. 内置的资源限制

CentOS 6: 除了ulimit,没有其他限制进程资源的简便方法

CentOS 7: 除了ulimit,还支持部分cgroup限制,可对进程做内存限制和cpu资源限制等

[Service]
ExecStart=...
MemoryLimit=500M
CPUShares=100

另外,CentOS 7可以通过systemd-cgtop命令查看cgroup里的性能数据

9. 服务异常自动重启

upstart

start on runlevel [3]

description "mytest"

exec /root/mytest.sh
post-stop exec sleep 5
respawn
respawn limit unlimited

systemd

[Unit]
Description=mytest [Service]
Type=simple
ExecStart=/root/mytest.sh
Restart=always
RestartSec=5
StartLimitInterval=0 [Install]
WantedBy=multi-user.target

上面2种方式均表示,无限次自动重启,每次重启前等待5秒

10. 写日志方式

CentOS 6: 自行输出到文件中,或通过syslog记录(如logger命令)

CentOS 7: 只要程序由systemd启动,只需将输出日志到标准输出或标准错误

  • 建议centos7只将应用程序的一些元信息输出到标准输出或标准错误,比如启动成功、启动失败等等
  • 不建议将业务日志输出到journal。因为journal中所有日志都存在一个文件中,会导致2个问题:
    • 如果没有做日志持久化,则默认存在内存中,会导致最多一半的内存被占用
    • 存储量很大,会导致查询其他日志很耗时
  • 解决办法:输出到syslog,[Service]支持StandardOutput=syslog

11. 指定每条日志级别

CentOS 6: 通过syslog将不同级别的日志输出到不同文件

CentOS 7: 只需在输出的每一行开头加<日志级别>,比如

echo '<0>hello, emerg'
echo '<1>hello, alert'
echo '<2>hello, crit'
echo '<3>hello, err'
echo '<4>hello, warning'
echo '<5>hello, notice'
echo '<6>hello, info'
echo '<7>hello, debug'

12. systemd日志永久保存

systemd日志默认保存在内存中,因此当服务器重启后,就无法通过journalctl来查看之前的日志,解决方法:

mkdir -p /var/log/journal
systemctl restart systemd-journald

最详细的CentOS 6与7对比(二):服务管理对比的更多相关文章

  1. 最详细的CentOS 6与7对比(三):性能测试对比

    本主题将从3个角度进行对比 常见设置(CentOS 6 vs CentOS 7) 服务管理(Sysvinit vs Upstart vs Systemd) 性能测试(cpu/mem/io/oltp) ...

  2. 最详细的CentOS 6与7对比(一):常见设置对比

    本主题将从3个角度进行对比 常见设置(CentOS 6 vs CentOS 7) 服务管理(Sysvinit vs Upstart vs Systemd) 性能测试(cpu/mem/io/oltp) ...

  3. 超详细!CentOS 7 + Hadoop3.0.0 搭建伪分布式集群

    超详细!CentOS 7 + Hadoop3.0.0 搭建伪分布式集群 ps:本文的步骤已自实现过一遍,在正文部分避开了旧版教程在新版使用导致出错的内容,因此版本一致的情况下照搬执行基本不会有大错误. ...

  4. Ubuntu 和 Redhat / Fedora 服务管理命令对比表(附Fedora16新的服务管理工具systemctl )

    以 apache/httpd 服务作为例子 任务 Red Hat / Fedora Ubuntu Ubuntu (with sysv-rc-conf or sysvconfig) 立即启动/停止某服务 ...

  5. CentOS(十)--与Linux文件和目录管理相关的一些重要命令②

    在结束了第二期的广交会实习之后,又迎来了几天休闲的日子,继续学习Linux.在上一篇随笔 Linux学习之CentOS(十七)--与Linux文件和目录管理相关的一些重要命令① 中,详细记录了与Lin ...

  6. centos linux系统日常管理3 服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,curl,ping ,telnet,traceroute ,dig ,nc,nmap,host,nethogs 第十六节课

    centos linux系统日常管理3  服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,cur ...

  7. CentOS下Docker与.netcore(二) 之 Dockerfile

    CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.netcore(二) 之 Dockerfile CentOS下Docker与.netcore(三)之 三剑客 ...

  8. 对比java和python对比

    对比java和python 对比java和python 2011年04月18日 1.难易度而言.python远远简单于java. 2.开发速度.Python远优于java 3.运行速度.java远优于 ...

  9. 管家婆财贸ERP系列功能对比财贸c3-c8-c9功能对比介绍

    管家婆财贸ERP系列功能对比财贸c3-c8-c9功能对比介绍 管家婆财贸ERP产品功能 序号 名称 说明 一 采购管理 对日常订货.入库.退货.估价入库等业务进行处理,多种方便灵活的订单定制方式,实现 ...

随机推荐

  1. USB 接口探测分类

    USB 接口探测分类 SDP (Standand Downstream Port) 标准下行接口 标准USB都支持的接口 这种端口的D+和D-线上具有15kΩ下拉电阻.限流值如上讨论:挂起时为2.5m ...

  2. Deutsch lernen (02)

    1. fließend a. 流利的 Meine französische Freundin spricht fließend Deutsch.     流动的 Der Verkehr wickelt ...

  3. sqlitManager

    @interface sqlitManager : NSObject +(instancetype)sharedSqlitManager; -(void)createDB; -(void)create ...

  4. PS通道的界面颜色设置

    编辑--首选项---界面--界面---选项---(勾选)以彩色显示通道(彩色显示)或者不勾选(为黑白色显示)

  5. javaee IO流打印一行的方式

    package Dayin; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.F ...

  6. git 还原到某次commit

    不可逆提交 一,reset 1.git log查看提交记录 git log 2.选择某次提交的commit ID,ctrl+c复制提交ID 3.使用git reset –hard 还原到某一次提交 g ...

  7. PAT_A1147#Heaps

    Source: PAT A1147 Heaps (30 分) Description: In computer science, a heap is a specialized tree-based ...

  8. Lua的热更新学习笔记_01

    热更新的的实现方式 1.使用lua脚本编写游戏的UI或者其他的逻辑 2.使用C#的反射技术 3.使用C#Light AssetBundle是什么? 1.unity提供一个资源更新技术,就是通过Asse ...

  9. C++引用、类型转换、类和对象(day03)

    十 C++的引用(Reference) 引用型函数参数 )将引用用于函数的参数,可以修改实参变量的值,同时也能减小函数调用的开销. )引用参数有可能意外修饰实参的值,如果不希望修改实参变量本身,可以将 ...

  10. python-flask-2 安装及设定 flask

    https://linoxide.com/linux-how-to/install-flask-python-ubuntu/ 1. prerequisites > create a new us ...