Last active
February 18, 2016 11:59
-
-
Save ten0s/24c76d6d89ab0c30cb49 to your computer and use it in GitHub Desktop.
Erlang escript + signal handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./main.sh | |
erl: started. | |
erl: working... | |
erl: working... | |
erl: working... | |
^Cerl: working... | |
erl: stopped. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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