0
votes

I have an Erlang application named tb that runs fine from Erlang command line by doing application:start(tb). Whereas when I try to invoke the same application from inside escript using os:cmd, the application doesn't seem to run. When i do a 'ps | grep beam', I see the beam.smp process running. But the application is not generating any output.What might be the problem? Is there a better way to start another erlang VM from inside escript?

Here's the code snippet:

net_kernel:start([tb_escript, shortnames]),
read_config_file(FName),
Cookie = get(cookie),
Node = get(node),
N = io_lib:format("~p",[Node]),
lists:flatten(N),
C = io_lib:format("~p",[Cookie]),
lists:flatten(C),
EBIN = "~/tb/ebin",
erlang:set_cookie(tb_escript,Cookie), 
os:cmd("erl -pa " ++ EBIN ++ " -sname " ++ N ++ " -detached " ++ " -setcookie " ++ C ++ " -s application start tb").
1
Can you try printing the value you're passing to os:cmd to see if it looks right? Also, lists:flatten(N) and lists:flatten(C) do not do anything as you're not using the return value.Dogbert
Dogbert, thanks a lot for quick reply. I modified the escript to use the output lists:flatten(). That doesn't seemed to have fixed the problem though. Here's the string being passed to os:cmd "erl -pa ~/tb/ebin -sname timer_bench_node -detached -setcookie rtetimer_dev -s application start tb" And the output for os:cmd() is empty list.Ram V
And if you run erl -pa ~/tb/ebin -sname timer_bench_node -detached -setcookie rtetimer_dev -s application start tb from the command line, it works perfectly fine?Dogbert
Heres what I found. When I start the application using eval , the application runs fine. erl -pa ~/tb/ebin -sname timer_bench_node -detached -setcookie rtetimer_dev -eval "application:start(tb)". Whereas it doesn't run when using -s option. I also verified this remoting into the process using -remsh and executing application:which_applications(). Only -eval case works, -s doesn't. Do you see similar behavior for any of your applications?Ram V
To elaborate on the last part, I can remote into the detached shell using -remsh in both cases. In case of -eval, I see my app listed when I run application:which_applications(). But when -s is used, I don't see the "tb" app. Do you see similar behavior for any of your applications?Ram V

1 Answers

0
votes

This happens because the args flag to -s wraps the arguments in a list and passes that to module:function/1. -s application start tb will execute application:start([tb]), which would return {error,{bad_application,[ssl]}}. As this is just a normal return value, no error is printed by erl.

From the erl documentation:

-s Mod [Func [Arg1, Arg2, ...]](init flag)

Makes init call the specified function. Func defaults to start. If no arguments are provided, the function is assumed to be of arity 0. Otherwise it is assumed to be of arity 1, taking the list [Arg1,Arg2,...] as argument. All arguments are passed as atoms.

There are two ways to solve this:

  1. Use -eval "application:start(tb)", as you already mentioned in a comment.

  2. Add a start/0 (if not already present) function to tb which calls application:start(tb), and then pass just -s tb to erl. -s with a single argument will call module:start().