在Elixir escript中捕获退出信号

时间:2017-02-19 06:16:59

标签: erlang elixir

我想编写一个escri,在收到HUP信号时重新加载其配置。当我启动escript时,我在OS X上并在Activity Monitor中搜索任何新进程。当我这样做时,弹出这些:inet_gethost(两次),erl_child_setup和beam.swp。当我向erl_child_setup发送一个SIGHUP时,它会以" erl_child_setup关闭"的消息崩溃。当我发送到beam.swp时,我收到一条消息" Hangup:1",但我的陷阱代码没有被调用。

以下是一些示例代码,说明了我要做的事情:

defmodule TrapHup do

  def main(args) do
    Process.flag(:trap_exit, true)
    main_loop()
  end

  def main_loop() do
    receive do
      { :EXIT, _from, reason } -> 
        IO.puts "Caught exit!"
        IO.inspect reason
        main_loop()
    end
  end
end

1 个答案:

答案 0 :(得分:0)

我发现只有Elixir / Erlang这是不可能的。显然,通过bash可以实现这个要点:https://gist.github.com/Djo/bfa9fa75928ce432ec51

以下是代码:

    FileInputStream credentialsFile = null;
    Scanner inFS = null;
    String credFile = ".\\src\\authenticationsystem\\credentials.txt";

    System.out.println("\nOpening file credentials.txt");

    credentialsFile = new FileInputStream(credFile);
    inFS = new Scanner(credentialsFile);
    String credentialLine = inFS.nextLine();
    System.out.println(credentialLine);
    System.out.println("Closing file credentials.txt\n");
    credentialsFile.close();

    String[] userCreds = credentialLine.split("\\s+");
    String userCred = userCreds[0];
    String userMD = userCreds[1];
    String userPass = userCreds[2];
    String userRole = userCreds[3];