掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
本主題將從3個角度進(jìn)行對比

成都創(chuàng)新互聯(lián)為客戶提供專業(yè)的成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項目涵蓋了網(wǎng)頁設(shè)計、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、手機網(wǎng)站開發(fā)等網(wǎng)站方面業(yè)務(wù)。
本文為第二部分:服務(wù)管理的對比
| / | CentOS 5 | CentOS 6 | CentOS 7 | 備注 |
|---|---|---|---|---|
| sysvinit | 第一代,傳統(tǒng),兼容最多(/etc/init.d/、/etc/rc.X) | |||
| upstart | 第二代,形似systemd雛形(/etc/init) | |||
| systemd | 第三代,配合cgroup,systemd完全接管整個系統(tǒng)(/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 |
| 關(guān)閉 | service mytest stop | initctl stop mytest | systemctl stop mytest.service |
| 強殺進(jìn)程 | 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中g(shù)rep,麻煩且效率低
CentOS 7: 統(tǒng)一使用journalctl,可以使用多個因素匹配,比如時間段、服務(wù)名、日志級別等等。另外,systemd日志默認(rèn)經(jīng)過壓縮,是二進(jìn)制文件,無法直接查看
| journalctl常用命令 | 作用 | CentOS 6比 |
|---|---|---|
| journalctl | 所有日志,包含系統(tǒng)、內(nèi)核等等 | 手動在對應(yīng)日志文件中g(shù)rep |
| journalctl --dmesg | 查看當(dāng)前開機后的內(nèi)核日志 | dmesg |
| journalctl --boot | 查看當(dāng)前開機后的日志 | 先查當(dāng)前開機啟動時間,然后cat /var/log/... |
| journalctl --boot=-1 | 查看上一次啟動的日志 | 查詢上次開機到當(dāng)前開機之間時間,然后cat /var/log/... |
| journalctl --since="2018-08-01 12:00:00" | 查看從指定時間開始到當(dāng)前的日志 | 手動在日志里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 | 查看指定服務(wù)日志 | 先查詢?nèi)罩颈4媛窂?,然后再cat查看 |
| journalctl -p 0 | 查看指定日志級別的日志,日志級別從0到7 | 通過syslog將不同級別的日志放到不同文件中 |
| journalctl -u mytest.service -o json-pretty或-o verbose | 查看每條日志詳細(xì)信息(包含元信息) | 無 |
| 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,沒有其他限制進(jìn)程資源的簡便方法
CentOS 7: 除了ulimit,還支持部分cgroup限制,可對進(jìn)程做內(nèi)存限制和cpu資源限制等
[Service]
ExecStart=...
MemoryLimit=500M
CPUShares=100
另外,CentOS 7可以通過systemd-cgtop命令查看cgroup里的性能數(shù)據(jù)
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啟動,只需將輸出日志到標(biāo)準(zhǔn)輸出或標(biāo)準(zhǔn)錯誤
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日志默認(rèn)保存在內(nèi)存中,因此當(dāng)服務(wù)器重啟后,就無法通過journalctl來查看之前的日志,解決方法:
mkdir -p /var/log/journal
systemctl restart systemd-journald 
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流