最详细的CentOS 6与7对比(二):服务管理对比

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

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

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

1. sysvinit、upstart、systemd简介

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

2. sysvinit、upstart、systemd常用命令

动作sysvinitupstartsystemd
查看service mytest statusinitctl status mytestsystemctl status mytest.service
启动service mytest startinitctl start mytestsystemctl start mytest.service
关闭service mytest stopinitctl stop mytestsystemctl stop mytest.service
强杀进程kill -9 PIDkill -9 PIDsystemctl kill mytest.service –signal=9
重启service mytest restartinitctl restart mytestsystemctl restart mytest.service
重载service mytest reloadinitctl reload mytestsystemctl reload mytest.service
开机启动chkconfig mytest on/etc/init/mytest.conf里配置start on runlevel [3]systemctl enable mytest.service

3. runlevel运行级别

运行级别CentOS 6CentOS 7
0haltrunlevel0.target -> poweroff.target
1Single user moderunlevel1.target -> rescue.target
2Multiuser, without NFSrunlevel2.target -> multi-user.target
3Full multiuser moderunlevel3.target -> multi-user.target
4unusedrunlevel4.target -> multi-user.target
5X11runlevel5.target -> graphical.target
6rebootrunlevel6.target -> reboot.target
查看cat /etc/inittabsystemctl get-default
开机生效编辑/etc/inittabsystemctl set-default multi-user.target
立即切换init 5systemctl 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/functionsstart() { … }
stop() { … }
restart() { … }
reload() { … }
status() { … }

case"$1"in
        start)
                start
                ;;
        stop)
                stop
                ;;
…
esacexit$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

starton runlevel [3]

description "mytest"

exec /root/mytest.sh
post-stop exec sleep5
respawn
respawn limitunlimited

systemd

[Unit]Description=mytest
 [Service]Type=simple
ExecStart=/root/mytest.sh
Restart=always
RestartSec=5StartLimitInterval=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

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/220492.html<

(0)
运维的头像运维
上一篇2025-04-14 14:34
下一篇 2025-04-14 14:35

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注