免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1112 | 回复: 0
打印 上一主题 下一主题

用inittab实现进程异常退出则自动重启 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:51 |只看该作者 |倒序浏览

写了个脚本,需要跑一个服务器,但是这个业务比较关键,不能down掉。怎么办呢?写一个daemon监视pid是否存在不存在则exec?daemon本身挂掉了怎么办?双进程互相监视?

无意中查到了一个Linux现有机制可以很好的完成这个问题;inittab

使用很简单,编辑 /etc/inittab 文件在最后一行写自己的东西就行了。语法可以照抄,格式为:

id:runlevels:action:进程名

id是不能重复的一个唯一标志

runlevel 就是进程运行级别。0是halt,1是单用户模式,6是重启。一般的程序在普通模式下的runlevel都是 2到5。Debian一般是2。可以参考wikipedia上的定义

action 就是采取的动作。分下面这么几种:

respawn
The process will be restarted whenever it terminates (e.g. getty).
wait
The process will be started once when the specified runlevel is entered and init will wait for its termination.
once
The process will be executed once when the specified runlevel is entered.
boot
The process will be executed during system boot. The runlevels field is ignored.
bootwait
The process will be executed during system boot, while init waits for its termination (e.g. /etc/rc). The runlevels field is ignored.
off
This does nothing.
ondemand
A process marked with an ondemand runlevel will be executed whenever the specified ondemand runlevel is called. However, no runlevel change will occur (ondemand runlevels are `a', `b', and `c').
initdefault
An initdefault entry specifies the runlevel which should be entered after system boot. If none exists, init will ask for a runlevel on the console. The process field is ignored.
sysinit
The process will be executed during system boot. It will be executed before any boot or bootwait entries. The runlevels field is ignored.
powerwait
The process will be executed when the power goes down. Init is usually informed about this by a process talking to a UPS connected to the computer. Init will wait for the process to finish before continuing.
powerfail
As for powerwait, except that init does not wait for the process's completion.
powerokwait
This process will be executed as soon as init is informormed that the power has been restored.
powerfailnow
This process will be executed when init is told that the battery of the external UPS is almost empty and the power is failing (provided that the external UPS and the monitoring process are able to detect this condition).
ctrlaltdel
The process will be executed when init receives the SIGINT signal. This means that someone on the system console has pressed the CTRL-ALT-DEL key combination. Typically one wants to execute some sort of shutdown either to get into single-user level or to reboot the machine.
kbrequest
The process will be executed when init receives a signal from the keyboard handler that a special key combination was pressed on the console keyboard.
The documentation for this function is not complete yet; more documentation can be found in the kbd-x.xx packages (most recent was kbd-0.94 at the time of this writing). Basically you want to map some keyboard combination to the "KeyboardSignal" action. For example, to map Alt-Uparrow for this purpose use the following in your keymaps file:

alt keycode 103 = KeyboardSignal

貌似很有用~~~

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP