Wednesday, March 4, 2009

Gracefully terminating erlang VM

Once started usually it is not required to stop erlang VM. We can stop the services running as well as erlang VM in a graceful manner with init:stop/0 function. Trick is to call this function on the target VM via remote shell.

For example, to stop the VM running as with cookie foo

erl -name -remsh -setcookie foo -s init stop

The above command will not work as init:stop() is executed within bar@localhost VM, not in foo@localhost. Another way is to do spawn in the target.

erl -name bar@localhost -remsh foo@localhost -setcookie foo -s proc_lib spawn '' init stop "" -s init stop

The above command will try to execute proc_lib:spawn( '', init, stop, []). There is a small caveat here. -s option pass all the paramter to proc_lib:spawn/4 as a list, not as a individual parameter.

proc_lib:spawn( ['', init, stop, [] ] ).

Since there is no such function defined, it will not work.

Only other solution is to create a module with one exported function which will spawn a function init:stop/0 on a remote shell.

-module (ctrl).


stop(Node) ->
proc_lib:spawn( hd(Node), init, stop, [] ).

Note that ctrl:stop/1 function gets list as an argument and it taking only the head element from it.

erl -name -remsh -setcookie foo -s ctrl stop '' -s init stop

The above command line execute ctrl:stop(['']), init:stop()

UPDATE: This can be done from command line without using any other module. All you have to do is to use -eval option on erl command line.

erl -name -setcookie foo -eval "rpc:call('', init, stop, []), init:stop()."


Christian said...

Or just

rpc:call(Node, init, stop, []).

Dude From Mangalore said...


Yogish Baliga said...

Thanx for the suggestion. I thought of sending the message via rpc:call from the command line using

erl -name -remsh -setcookie foo -s rpc call '' init stop "" -init stop

This won't work as this is translated to

rpc:call(['', init, stop, ""])

There is no such function rpc:call/1

Book Promotion