这个问题与我之前的问题有关:将erlang shell作为守护进程/服务运行
我有一个脚本,看起来像这样:#!/bin/bash
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
export HEART_COMMAND="/etc/init.d/script restart"
start() {
erl -heart -pa DIR -sname NAME -setcookie COOKIE -env port 21 -s M -s M2 --
### Create the lock file ###
touch /var/lock/lock
}
stop() {
erl -noshell -sname temp_control -setcookie COOKIE -eval "rpc:call(NAME@ubuntu, init, stop, [])" -s init stop
### Now, delete the lock file ###
rm -f /var/lock/lock
}
### main logic ###
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
# start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
我不知道如何模拟崩溃,所以我只是尝试ctrl+c并中止shell,输出看起来像这样:
root@ubuntu:/etc/init.d# ./script start
heart_beat_kill_pid = 17512
Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.4 (abort with ^G)
(NAME@ubuntu)1> Starting M2
Listening on port 21
(NAME@ubuntu)1>
(NAME@ubuntu)1>
(NAME@ubuntu)1>
(NAME@ubuntu)1>
(NAME@ubuntu)1>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
(v)ersion (k)ill (D)b-tables (d)istribution
a
heart: Fri Jul 29 09:25:10 2011: Erlang has closed.
root@ubuntu:/etc/init.d# heart_beat_kill_pid = 17557
heart: Fri Jul 29 09:25:13 2011: Erlang has closed.
/etc/init.d/NAME: line 20: 17557 Killed erl -heart -pa DIR -sname NAME -setcookie COOKIE -env port 21 -s M -s M2 --
heart: Fri Jul 29 09:25:13 2011: Executed "/etc/init.d/script restart". Terminating.
heart_beat_kill_pid = 17602
heart: Fri Jul 29 09:25:15 2011: Erlang has closed.
/etc/init.d/NAME: line 20: 17602 Killed erl -heart -pa DIR -sname NAME -setcookie COOKIE -env port 21 -s M -s M2 --
heart: Fri Jul 29 09:25:15 2011: Executed "/etc/init.d/script restart". Terminating.
heart: Fri Jul 29 09:25:17 2011: Executed "/etc/init.d/script restart". Terminating.
root@ubuntu:/etc/init.d#
如果我不注释启动它的脚本中的代码行,它就会永远继续下去。这就像终止erlang shell的无尽循环…什么的。
如果我尝试例如"export HEART_COMMAND="/bin/echo hello",它会显示"写错误:管道破裂"。
为什么不工作?如何正确地模拟崩溃以检查心脏命令是否有效?
谢谢你给我的建议。
回答你没有问的问题(但提到了几次你不知道怎么做)
模拟崩溃,所以kill -SEGV <PID>
的例子:
$ sleep 30 &
[1] 13274
$ kill -SEGV 13274
[1]+ Segmentation fault sleep 30
也,所以,虽然我不知道erlang, 我假定,它产生多个线程,一个线程可以监视另一个通过发送心跳消息。如果另一个线程没有响应,则假定它被挂起并重新启动。