停止并再次运行erlang中的应用程序

时间:2013-02-27 13:40:00

标签: erlang

有一个在Erlang中开发的应用程序并运行我需要做的应用程序: ./ build.sh&& ./deploy.sh& amp;& ./erl_start.sh

我想创建一个与 Ctrl + C 完全相同的功能并运行 ./ build.sh&& ./deploy.sh& amp;& ./erl_start.sh

意味着停止应用程序并再次运行此应用程序

我尝试使用此功能:

verify(Val)->
    if Val =:=40 ->
Pid = self(), 

io:format("~w~n",[Pid]),
spawn(fun() -> exit(Pid, kill) end),
    LsOut = os:cmd("./build.sh && ./deploy.sh && ./erl_start.sh");

    true -> ok

    end.

当我测试这个功能时,我有这个错误:

1> model:verify(40).
<0.144.0>
** exception exit: killed

我测试了这个命令:

1> [io:format("~p : ~p~n",[Pid,erlang:process_info(Pid,current_function)]) || Pid <- processes()].
<0.0.0> : {current_function,{init,loop,1}}
<0.3.0> : {current_function,{erl_prim_loader,loop,3}}
<0.5.0> : {current_function,{gen_event,fetch_msg,5}}
<0.6.0> : {current_function,{gen_server,loop,6}}
<0.8.0> : {current_function,{application_master,main_loop,2}}
<0.9.0> : {current_function,{application_master,loop_it,4}}
<0.10.0> : {current_function,{gen_server,loop,6}}
<0.11.0> : {current_function,{gen_server,loop,6}}
<0.12.0> : {current_function,{gen_server,loop,6}}
<0.13.0> : {current_function,{global,loop_the_locker,1}}
<0.14.0> : {current_function,{global,collect_deletions,2}}
<0.15.0> : {current_function,{global,loop_the_registrar,0}}
<0.16.0> : {current_function,{gen_server,loop,6}}
<0.18.0> : {current_function,{gen_server,loop,6}}
<0.19.0> : {current_function,{gen_server,loop,6}}
<0.20.0> : {current_function,{code_server,loop,1}}
<0.21.0> : {current_function,{gen_server,loop,6}}
<0.22.0> : {current_function,{standard_error,server_loop,1}}
<0.23.0> : {current_function,{gen_server,loop,6}}
<0.24.0> : {current_function,{user_drv,server_loop,5}}
<0.25.0> : {current_function,{group,server_loop,3}}
<0.26.0> : {current_function,{group,server_loop,3}}
<0.27.0> : {current_function,{shell,shell_rep,4}}
<0.28.0> : {current_function,{gen_server,loop,6}}
<0.29.0> : {current_function,{gen_server,loop,6}}
<0.36.0> : {current_function,{application_master,main_loop,2}}
<0.37.0> : {current_function,{application_master,loop_it,4}}
<0.38.0> : {current_function,{gen_server,loop,6}}
<0.39.0> : {current_function,{gen_event,fetch_msg,5}}
<0.40.0> : {current_function,{gen_server,loop,6}}
<0.41.0> : {current_function,{gen_server,loop,6}}
<0.44.0> : {current_function,{gen_server,loop,6}}
<0.45.0> : {current_function,{mnesia_locker,loop,1}}
<0.46.0> : {current_function,{gen_server,loop,6}}
<0.47.0> : {current_function,{mnesia_tm,doit_loop,1}}
<0.48.0> : {current_function,{gen_server,loop,6}}
<0.49.0> : {current_function,{gen_server,loop,6}}
<0.53.0> : {current_function,{gen_server,loop,6}}
<0.54.0> : {current_function,{gen_server,loop,6}}
<0.63.0> : {current_function,{disk_log,loop,1}}
<0.65.0> : {current_function,{gen_server,loop,6}}
<0.66.0> : {current_function,{gen_server,loop,6}}
<0.67.0> : {current_function,{gen_server,loop,6}}
<0.68.0> : {current_function,{gen_server,loop,6}}
<0.69.0> : {current_function,{mnesia_late_loader,loop,1}}
<0.105.0> : {current_function,{application_master,main_loop,2}}
<0.106.0> : {current_function,{application_master,loop_it,4}}
<0.107.0> : {current_function,{gen_server,loop,6}}
<0.108.0> : {current_function,{gen_server,loop,6}}
<0.109.0> : {current_function,{gen_server,loop,6}}
<0.110.0> : {current_function,{gen_server,loop,6}}
<0.111.0> : {current_function,{gen_server,loop,6}}
<0.112.0> : {current_function,{gen_server,loop,6}}
<0.113.0> : {current_function,{gen_server,loop,6}}
<0.114.0> : {current_function,{gen_server,loop,6}}
<0.115.0> : {current_function,{gen_server,loop,6}}
<0.117.0> : {current_function,{gen_server,loop,6}}
<0.118.0> : {current_function,{gen_server,loop,6}}
<0.119.0> : {current_function,{gen_server,loop,6}}
<0.120.0> : {current_function,{gen_server,loop,6}}
<0.121.0> : {current_function,{prim_inet,accept0,2}}
<0.123.0> : {current_function,{gen_server,loop,6}}
<0.124.0> : {current_function,{gen_server,loop,6}}
<0.125.0> : {current_function,{gen_server,loop,6}}
<0.126.0> : {current_function,{gen_server,loop,6}}
<0.127.0> : {current_function,{prim_inet,accept0,2}}
<0.129.0> : {current_function,{gen_server,loop,6}}
<0.130.0> : {current_function,{gen_server,loop,6}}
<0.131.0> : {current_function,{gen_server,loop,6}}
<0.132.0> : {current_function,{gen_server,loop,6}}
<0.133.0> : {current_function,{prim_inet,accept0,2}}
<0.135.0> : {current_function,{gen_server,loop,6}}
<0.136.0> : {current_function,{gen_server,loop,6}}
<0.137.0> : {current_function,{gen_server,loop,6}}
<0.138.0> : {current_function,{gen_server,loop,6}}
<0.139.0> : {current_function,{prim_inet,accept0,2}}
<0.140.0> : {current_function,{gen_server,loop,6}}
<0.143.0> : {current_function,{os,start_port_srv_loop,2}}
<0.144.0> : {current_function,{erl_eval,do_apply,5}}
[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,
 ok,ok,ok,ok,ok,ok,ok,ok,ok,ok|...]

我也测试了这个命令:

1> [io:format("~p : ~p~n",[Pid,erlang:process_info(Pid,initial_call)]) || Pid <- processes()]. 
<0.0.0> : {initial_call,{otp_ring0,start,2}}
<0.3.0> : {initial_call,{erlang,apply,2}}
<0.5.0> : {initial_call,{proc_lib,init_p,5}}
<0.6.0> : {initial_call,{erlang,apply,2}}
<0.8.0> : {initial_call,{proc_lib,init_p,5}}
<0.9.0> : {initial_call,{application_master,start_it,4}}
<0.10.0> : {initial_call,{proc_lib,init_p,5}}
<0.11.0> : {initial_call,{proc_lib,init_p,5}}
<0.12.0> : {initial_call,{proc_lib,init_p,5}}
<0.13.0> : {initial_call,{erlang,apply,2}}
<0.14.0> : {initial_call,{erlang,apply,2}}
<0.15.0> : {initial_call,{erlang,apply,2}}
<0.16.0> : {initial_call,{proc_lib,init_p,5}}
<0.18.0> : {initial_call,{proc_lib,init_p,5}}
<0.19.0> : {initial_call,{proc_lib,init_p,5}}
<0.20.0> : {initial_call,{erlang,apply,2}}
<0.21.0> : {initial_call,{proc_lib,init_p,5}}
<0.22.0> : {initial_call,{standard_error,server,2}}
<0.23.0> : {initial_call,{proc_lib,init_p,5}}
<0.24.0> : {initial_call,{user_drv,server,2}}
<0.25.0> : {initial_call,{group,server,3}}
<0.26.0> : {initial_call,{group,server,3}}
<0.27.0> : {initial_call,{erlang,apply,2}}
<0.28.0> : {initial_call,{proc_lib,init_p,5}}
<0.29.0> : {initial_call,{proc_lib,init_p,5}}
<0.36.0> : {initial_call,{proc_lib,init_p,5}}
<0.37.0> : {initial_call,{application_master,start_it,4}}
<0.38.0> : {initial_call,{proc_lib,init_p,5}}
<0.39.0> : {initial_call,{proc_lib,init_p,5}}
<0.40.0> : {initial_call,{proc_lib,init_p,5}}
<0.41.0> : {initial_call,{proc_lib,init_p,5}}
<0.44.0> : {initial_call,{proc_lib,init_p,5}}
<0.45.0> : {initial_call,{proc_lib,init_p,5}}
<0.46.0> : {initial_call,{proc_lib,init_p,5}}
<0.47.0> : {initial_call,{proc_lib,init_p,5}}
<0.48.0> : {initial_call,{proc_lib,init_p,5}}
<0.49.0> : {initial_call,{proc_lib,init_p,5}}
<0.53.0> : {initial_call,{proc_lib,init_p,5}}
<0.54.0> : {initial_call,{proc_lib,init_p,5}}
<0.63.0> : {initial_call,{proc_lib,init_p,5}}
<0.65.0> : {initial_call,{proc_lib,init_p,5}}
<0.66.0> : {initial_call,{proc_lib,init_p,5}}
<0.67.0> : {initial_call,{proc_lib,init_p,5}}
<0.68.0> : {initial_call,{proc_lib,init_p,5}}
<0.69.0> : {initial_call,{proc_lib,init_p,5}}
<0.105.0> : {initial_call,{proc_lib,init_p,5}}
<0.106.0> : {initial_call,{application_master,start_it,4}}
<0.107.0> : {initial_call,{proc_lib,init_p,5}}
<0.108.0> : {initial_call,{proc_lib,init_p,5}}
<0.109.0> : {initial_call,{proc_lib,init_p,5}}
<0.110.0> : {initial_call,{proc_lib,init_p,5}}
<0.111.0> : {initial_call,{proc_lib,init_p,5}}
<0.112.0> : {initial_call,{proc_lib,init_p,5}}
<0.113.0> : {initial_call,{proc_lib,init_p,5}}
<0.114.0> : {initial_call,{proc_lib,init_p,5}}
<0.115.0> : {initial_call,{proc_lib,init_p,5}}
<0.117.0> : {initial_call,{proc_lib,init_p,5}}
<0.118.0> : {initial_call,{proc_lib,init_p,5}}
<0.119.0> : {initial_call,{proc_lib,init_p,5}}
<0.120.0> : {initial_call,{proc_lib,init_p,5}}
<0.121.0> : {initial_call,{proc_lib,init_p,5}}
<0.123.0> : {initial_call,{proc_lib,init_p,5}}
<0.124.0> : {initial_call,{proc_lib,init_p,5}}
<0.125.0> : {initial_call,{proc_lib,init_p,5}}
<0.126.0> : {initial_call,{proc_lib,init_p,5}}
<0.127.0> : {initial_call,{proc_lib,init_p,5}}
<0.129.0> : {initial_call,{proc_lib,init_p,5}}
<0.130.0> : {initial_call,{proc_lib,init_p,5}}
<0.131.0> : {initial_call,{proc_lib,init_p,5}}
<0.132.0> : {initial_call,{proc_lib,init_p,5}}
<0.133.0> : {initial_call,{proc_lib,init_p,5}}
<0.135.0> : {initial_call,{proc_lib,init_p,5}}
<0.136.0> : {initial_call,{proc_lib,init_p,5}}
<0.137.0> : {initial_call,{proc_lib,init_p,5}}
<0.138.0> : {initial_call,{proc_lib,init_p,5}}
<0.139.0> : {initial_call,{proc_lib,init_p,5}}
<0.140.0> : {initial_call,{proc_lib,init_p,5}}
<0.143.0> : {initial_call,{erlang,apply,2}}
<0.144.0> : {initial_call,{erlang,apply,2}}
[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,
 ok,ok,ok,ok,ok,ok,ok,ok,ok,ok|...]

我尝试使用此功能:

verify(Val) ->
    erlang:halt();

verify(_) ->
    % do nothing

   ok.

当我测试时我得到了这个结果:

1>  model:verify(40).
root@ubuntu:/home/afif/Desktop/ttest/erlang# 

它只退出VM

但正如我已经说过我想停止应用程序并再次运行此应用程序

为此我尝试:

verify(Val) ->
        erlang:halt(),
    LsOut = os:cmd("./build.sh && ./deploy.sh && ./erl_start.sh");
    verify(_) ->
        % do nothing

       ok.

但是我的结果与之前的代码

相同

我所做的只是工作的一半 仍然是如何再次运行该程序

1 个答案:

答案 0 :(得分:0)

如果您只想退出VM以重新启动,则可以使用函数erlang:halt()或erlang:halt(Status)。来自文档:

  

<强>停止()

     

暂停Erlang运行时系统并指示正常退出   呼唤环境。没有回报价值。

     
    

停止()。

         
      

os_prompt%

    
         

<强>停止(状态)

         

类型:

         
      

状态=整数()&gt; = 0 |串()

    
  
     

状态必须是非负整数或字符串。停止Erlang   运行时系统。没有回报价值。如果Status是一个整数,那么它是   作为Erlang的退出状态返回到调用环境。如果   Status是一个字符串,生成一个带String的Erlang崩溃转储   标语,然后以非零状态代码退出。

     

请注意,在许多平台上,仅支持状态代码0-255   由操作系统。

所以你的代码可能是:

verify(40) ->
    erlang:halt();
verify(_) ->
    % do nothing
    ok.

但正如我在上一个问题中告诉你的那样,我认为你没有朝着正确的方向前进。