init 管理 ssh 服务
RHEL 6 / CentOS 6 开始使用 upstart 管理服务,而 RHEL 5 / CentOS 5 则使用传统的 SysV 进程管理,最新的 CentOS 7 则使用的是 Systemd(Systemd 本身具备失败重启机制,这是它的先天优势)。
为防止进程意外退出,可以使用 init 来管理监控指定的程序,防止 ssh 意外退出,方法如下:
1. RHEL 5 / CentOS 5
在 /etc/inittab 中加入如下字段:
ssh:2345:respawn:/usr/sbin/sshd -f /etc/ssh/sshd_config -D # 管理 ssh 服务
加入以上配置之后,执行 init q 即可生效。
可以查看手册 man 5 inittab :
...
id:runlevels:action:process
...
respawn # 关于 action respawn 的介绍
The process will be restarted whenever it terminates (e.g.getty).
...
2. RHEL 6 / CentOS 6
在 /etc/init/ 下创建 .conf 结尾的文件,如创建 ssh.conf , cat /etc/init/ssh.conf 内容如下:
#!/bin/bash
# ssh
#
# This service starts sshd
start on runlevel [2345]
stop on runlevel [!RUNLEVEL]
# kill timeout 30
console output
respawn
exec /usr/sbin/sshd -f /etc/ssh/sshd_config -D
执行 start ssh 启动服务, stop ssh 停止程序。通过 initctl list 可以查看当前 init 监控管理的服务。