Skip to content

Instantly share code, notes, and snippets.

@ten0s
Last active February 18, 2016 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ten0s/24c76d6d89ab0c30cb49 to your computer and use it in GitHub Desktop.
Save ten0s/24c76d6d89ab0c30cb49 to your computer and use it in GitHub Desktop.
Erlang escript + signal handling
$ ./main.sh
erl: started.
erl: working...
erl: working...
erl: working...
^Cerl: working...
erl: stopped.
#!/usr/bin/env escript
-export([
main/1
]).
main([Node, Cookie]) ->
{ok, _} = net_kernel:start([list_to_atom(Node), shortnames]),
true = erlang:set_cookie(node(), list_to_atom(Cookie)),
Self = self(),
Worker = spawn(fun() -> loop(Self) end),
true = register(worker, Worker),
io:format("erl: started.~n"),
receive
{stopped, Worker} ->
io:format("erl: stopped.~n")
after infinity ->
ok
end.
loop(Notifee) ->
io:format("erl: working...~n"),
receive
stop ->
Notifee ! {stopped, self()},
ok
after
1000 ->
loop(Notifee)
end.
#!/bin/bash
node=${RANDOM}
host=$(hostname)
cookie=test
function notify_erl {
erl -sname ${RANDOM} -setcookie ${cookie} -noshell -eval "{worker, '"${node}@${host}"'} ! stop, halt(0)."
exit 1
}
trap notify_erl SIGHUP SIGINT SIGTERM
./main.es ${node} ${cookie} &
while true; do sleep 1; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment