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

成都创新互联为客户提供专业的成都网站设计、成都网站制作、程序、域名、空间一条龙服务,提供基于WEB的系统开发. 服务项目涵盖了网页设计、网站程序开发、WEB系统开发、微信二次开发、手机网站开发等网站方面业务。
本文为第二部分:服务管理的对比
| / | CentOS 5 | CentOS 6 | CentOS 7 | 备注 |
|---|---|---|---|---|
| sysvinit | 第一代,传统,兼容最多(/etc/init.d/、/etc/rc.X) | |||
| upstart | 第二代,形似systemd雏形(/etc/init) | |||
| systemd | 第三代,配合cgroup,systemd完全接管整个系统(/usr/lib/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 |
| 运行级别 | 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 |
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等 |
CentOS 6
CentOS 7
sysvinit
cat > /etc/init.d/mytest <
upstart
cat > /etc/init/mytest.conf <
systemd
cat > /usr/lib/systemd/system/mytest.service <
CentOS 6: 除了ulimit,没有其他限制进程资源的简便方法
CentOS 7: 除了ulimit,还支持部分cgroup限制,可对进程做内存限制和cpu资源限制等
[Service]
ExecStart=...
MemoryLimit=500M
CPUShares=100
另外,CentOS 7可以通过systemd-cgtop命令查看cgroup里的性能数据
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秒
CentOS 6: 自行输出到文件中,或通过syslog记录(如logger命令)
CentOS 7: 只要程序由systemd启动,只需将输出日志到标准输出或标准错误
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'
systemd日志默认保存在内存中,因此当服务器重启后,就无法通过journalctl来查看之前的日志,解决方法:
mkdir -p /var/log/journal
systemctl restart systemd-journald