[erlang-questions] restart erlang app service - kills rest of running code

Dave Cottlehuber dave@REDACTED
Thu Oct 20 20:57:42 CEST 2011

On 20 October 2011 18:41, Wes James <comptekki@REDACTED> wrote:
> I put an erlang VM on a win 7 workstation as a service:
> erlsrv add "0esvc" -stopaction "init:stop()." -name node@REDACTED -w
> "C:/erldir" -args "-setcookie CHOCOLATECHIP -kernel
> inet_dist_listen_min 11111 -s my_app"

I don't think stopaction is needed. The vm receives windows service
control messages and responds to them just fine.

> I then send a message from my mac to the windows workstation:
> my_app:do_send(net_restart,node@REDACTED).
> and it gets to this point:
> case Com of
>        net_restart ->
>            os:cmd("net stop 0esvc"),
> % but then everything stops after the above line - the next line is
> not reached since the erlang VM has been stopped.

Not surprising considering net.exe tells the OS to stop the VM. Were
you expecting the code to keep running even though the VM is stopped?

>            os:cmd("net start 0esvc");
> I could write a script 0esvc.cmd with
> net stop 0esvc
> net start 0esvc
> instead and call os:cmd("0esvc.cmd");
> but is there a way to restart the 0esvc from erlang?

Why not call init:restart() which does exactly what you need, from
inside the VM?

> For now I have a second simple service 2esvc that I can send a command
> and it restarts the 0esvc.
> How about if I write an erlang script that has
> timer:sleep(5000),
> os:cmd("net start 0esvc");
> And I call it:
> net_restart ->
>    os:cmd("escript restart.erl"),
>    os:cmd("net stop 0esvc"),
> But then the escript call will block the process from continuing until
> os:cmd is done - is there a way to spawn that call off so it can run
> later?  But to be spawned the VM needs to be running.
> Maybe 0esvc.cmd is the best way to do this.
> Why am I restarting the VM?  If I update the code in my_app,
> restarting will update it.  I guess, I could just not worry about the
> restart and when code is updated, just restart the workstation (one of
> 50 in a student computer lab).

Would erlang's hot code loading allow you to avoid needing to restart
the VM at all in your case?

Finally consider setting the service parameters to auto-restart with:

-onfail restart_always

in your erlsrv service creation.


More information about the erlang-questions mailing list