【问题标题】:Erlang: invoking erl -eval from command line never exitsErlang:从命令行调用 erl -eval 永远不会退出
【发布时间】:2013-05-28 03:12:31
【问题描述】:

我有一个简单的 Erlang 命令,我想通过 erl -eval 调用它(编译 erlydtl 模板,如 erlydtl page 所述)。

当我从 shell 以交互方式执行此操作时,一切正常,命令立即退出:

erl -pa ebin deps\erlydtl\ebin Eshell V5.9.3.1  (abort with ^G) 
1> erlydtl:compile('templates/tictactoe.dtl',tictactoe_dtl,[{out_dir,'ebin'}]).
ok

但是当我尝试通过erl -eval 执行此操作时(我想从 .bat 文件运行它):

erl -pa ebin deps\erlydtl\ebin -noshell -eval erlydtl:compile('templates/tictactoe.dtl',tictactoe_dtl,[{out_dir,'ebin'}])

然后命令完成它的工作(模板已编译)但它没有退出,我需要使用 ctrl+c 手动终止 shell 进程(我在 Windows 下工作)。

我只希望命令编译模板并退出。可能是什么问题?

更新:

一种解决方案可能是在命令末尾附加 exit() 调用,但我最终得到以下结果:

erl -pa ebin deps\erlydtl\ebin -noshell -eval erlydtl:compile('templates/tictactoe.dtl',tictactoe_dtl,[{out_dir,'ebin'}]),exit(success).
{"init terminating in do_boot",success}

Crash dump was written to: erl_crash.dump
init terminating in do_boot (success)

错误信息很烦人,所以我还是不喜欢这个解决方案。

【问题讨论】:

  • 另外,您可以使用钢筋构建项目。放在 /templates 目录下支持 Erlydtl 模板编译。
  • 我目前使用 rebar 但正在寻找替代方案,因为 rebar 编译工作时间很长。
  • rebar compile skip_deps=true 对你来说也很慢吗?
  • @WardBekker 感谢您的关注,沃德!是的,我知道skip_deps=true,我正在使用它。但是rebar compile skip_deps=true 完成我非常小的玩具项目需要大约 5 秒左右,即使自上次构建以来没有任何更改!而且由于它是玩具项目,我需要经常进行编译-运行循环。使用erl -make 编译对我来说只需要不到 1 秒的时间,并且只重新编译编辑过的文件,所以我决定切换到它并编写快速的小脚本来编译 erlydtl 模板。我们在这里:-)。

标签: erlang erlang-shell erl


【解决方案1】:

您需要指定 -noshell 并在最后调用 halt()。比如

erl -noshell -eval "erlydtl:compile('templates/tictactoe.dtl',tictactoe_dtl,[{out_dir,'ebin'}]),halt()."

【讨论】:

  • 总的来说,我建议不要使用停止,因为这等于让你崩溃。首选方法是通过调用 init:stop/0 来终止 BEAM,因为这允许应用程序进行任何必要的清理。在这种情况下,它会使整个执行过程变慢一些,但是使用 erlang:halt/0 作为第一个调用端口是一个坏习惯。我见过其他熟练的 Erlang 程序员在调试上浪费时间,仅仅是因为他们养成了使用 erlang:halt/0 的习惯。
【解决方案2】:

这应该可以解决问题

erl -noshell -eval 'c:ls()' -eval 'init:stop()'

你必须告诉虚拟机关闭。

【讨论】:

  • 现在完美运行!感谢 Jan 的回答,并解释了为什么 halt() 是一个错误的解决方案。
猜你喜欢
  • 2016-09-14
  • 1970-01-01
  • 2020-09-27
  • 2011-03-24
  • 2018-06-09
  • 1970-01-01
  • 2021-09-18
  • 2016-11-27
  • 2022-08-03
相关资源
最近更新 更多