標籤:
erlang shell是使用者與 erlang 運行時系統互動的介面程式。事實上,erlang VM的運行不依賴任何shell,只要在啟動的時候添加參數detached就可以脫離終端。-
detachedStarts the Erlang runtime system detached from the system console. Useful for running daemons and backgrounds processes. Implies -noinput.實際上,detached 等效於noshell 加上 noinput。# erl -
detached -emu_args
Executing: /home/erl/lib/erlang/erts-5.10.3/bin/beam /home/erl/lib/erlang/erts-5.10.3/bin/beam -- -root /home/erl/lib/erlang -progname erl -- -home /root --
-noshell -noinput
另外,需要注意的是,windows不直接支援detached,而是以服務的形式在後台運行,見 erlsrv
現在討論erlang 接入遠程shell控制台的幾種方式。作業(JCL )模式 在 Erlang shell 中按下^G鍵,就可以看到作業控制模式(JCL mode)的菜單。在菜單中,有個選項能讓我們串連到一個遠程 shell。先以detached運行一個節點1:# erl -name
[email protected] -setcookie 123456 -detached檢查這個erlang進程是否運行
# ps -ef | grep beamroot 20672 1 0 01:32 ? 00:00:00 /home/erl/lib/erlang/erts-5.10.3/bin/beam -- -root /home/erl/lib/erlang -progname erl -- -home /root -- -name
[email protected] -setcookie 123456 -noshell -noinput 啟動另外一個節點2,並接入到節點1
# erl -name [email protected] -setcookie 123456 Erlang R16B02 (erts-5.10.3) [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false] Eshell V5.10.3 (abort with ^G) ([email protected])1> User switch command --> h c [nn] - connect to job i [nn] - interrupt job k [nn] - kill job j - list all jobs s [shell] - start local shell r [node [shell]] - start remote shell q - quit erlang ? | h - this message --> r ‘[email protected]‘ --> j 1 {shell,start,[init]} 2* {‘[email protected]‘,shell,start,[]} --> c 2 Eshell V5.10.3 (abort with ^G) ([email protected])1> |
注意了,windows下要使用werl
串連到遠程 shell 後,所有的終端輸入解析操作都由本地 shell 完成,不過求值的工作是在遠 程完成的。遠程求值的結果輸出全部被轉寄給本地 shell。 要退出 shell, 按^G回到 JCL 模式。 終端輸入解析操作是在本地進行的, 因此通過^G q 的方式退出 shell 是安全的。Eshell V5.10.3 (abort with ^G)
([email protected])1>
User switch command
--> q
Remsh 模式Remsh和 JCL 模式很類似,但是調用方式不同的機制。使用這種機制,JCL 模式的所有 操作步驟都可以被繞過,只需像下面這樣啟動 shell,對於長名字:
-remsh NodeStarts Erlang with a remote shell connected to Node.
以下方式啟動節點2,將直接接入節點1控制台:# erl -name
[email protected] -setcookie 123456
-remsh [email protected]Erlang R16B02 (erts-5.10.3) [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]Eshell V5.10.3 (abort with ^G)(
[email protected])1> node().‘[email protected]‘這種方式和JCL很相像,本地也會啟動一個erlang節點用於接入遠程shell
SSH 模式erlang內建了SSH的功能,我們可以很方便的開啟SSH服務,對外提供遠程 shell服務。 SSH的使用需要開啟crypto,如果erlang顯示以下錯誤,可以參考這篇文章。1> crypto:start(). ** exception error: undefined function crypto:start/0要使用該功能,通常需要先準備好具有遠端存取 SSH 許可權的 key,不過這裡為了快速測試,可以這樣做:節點1啟動ssh服務:
| Eshell V5.10.3 (abort with ^G)([email protected])1> ssh:start().ok([email protected])2> ssh:daemon(8888, [{password, "12345"}]).{ok,<0.57.0>} |
本地不需要啟動erlang節點,直接使用ssh串連即可,輸入以上設定的密碼,就可以接入節點1的shell控制台。
| # ssh -p 8888 [email protected]The authenticity of host ‘[127.0.0.1]:8888 ([127.0.0.1]:8888)‘ can‘t be established.RSA key fingerprint is ad:03:b4:6b:df:51:97:23:dc:47:cb:75:85:15:44:89.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added ‘[127.0.0.1]:8888‘ (RSA) to the list of known hosts.[email protected]‘s password:Eshell V5.10.3 (abort with ^G)([email protected])1> |
這種方式,erlang shell所有操作都是在遠程節點完成的。
管道(pipe)模式在使用管道(pipe)串連到一個Erlang節點時,和SSH一樣不需要啟動本地erlang節點。這種方法很少用,每次輸出時都調用fsync,如果輸出過多時,會有很大的效能損失。
具體做法為:用 run_erl 啟動 erlang,相當於把 erlang 進程包在一個管道中:# mkdir /tmp/erl_log# cd /home/erl/bin# ./run_erl -daemon /tmp/erl_pipe /tmp/erl_log "erl -name
[email protected] -setcookie 123456"其中,daemon 表示以後台進程運行,/tmp/erl_pipe是管道檔案的名稱,/tmp/erl_log指定了日誌儲存檔案夾然後使用 to_erl 程式來串連節點: # ./to_erl /tmp/erl_pipeAttaching to /tmp/erl_pipe (^D to exit)
(
[email protected])1> node().
‘[email protected]‘
參考:http://blog.csdn.net/mycwq/article/details/43850735https://s3.amazonaws.com/erlang-in-anger/text.v1.0.3.pdf
erlang 接入遠程shell控制台