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

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日志永久保存

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11649.html