This question is related to my previous one: Running erlang shell as a daemon/service
I have a script that looks like this:
#!/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
I don't know how to simulate a crash so I just tried ctrl+c and aborted the shell, the output looks like this:
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#
This goes on for ever if I don't comment the line of code in the script that starts it. It's like an endless loop of terminating erlang shells... or something.
If I try for example "export HEART_COMMAND="/bin/echo hello" it says "write error: broken pipe".
Why doesn't it work? How do I simulate a crash properly to check if the heart command work?
Thankful for any advice you might have.
Answering the question you didn't ask (but mentioned a couple of times that you don't know how to do)
To simulate a crash so kill -SEGV <PID>
Example:
$ sleep 30 &
[1] 13274
$ kill -SEGV 13274
[1]+ Segmentation fault sleep 30
Also, So while I don't know erlang, I presume that it spawns multiple threads and one thread can monitor another by sending heartbeat messages. If the other thread does not respond, it is assumed to be hung and restarted.