37

I know we can exit the IEX console with control-C. I'm curious if there's a command to type into the console that would also do the same thing.

jwfearn
  • 28,781
  • 28
  • 95
  • 122
User314159
  • 7,733
  • 9
  • 39
  • 63

5 Answers5

34

I can think of 3 ways to quit an IEx shell:

  1. The mentioned <ctrl-c> hit twice or once followed by q and then <enter>,
  2. <ctrl-g> and then q + <enter>,
  3. and finally System.halt,

but there is a difference between System.halt and the others.

Namely that System.halt "halts the Erlang runtime" and the others just "quit the shell".

When you have only one shell session running or the session is not attached to a separate runtime then both ways will produce the same result. But if you have a session connected to a separate runtime e.g. via iex --remsh (remote shell) then running System.halt in it will halt the runtime and so make both shell processes / runtimes terminate. Just quitting a shell (via method 1. or 2.) will not stop the runtime it is connected to.

Conclusion: if you connect with your shell to other runtimes then know that System.halt will halt the runtime you have connected to. If you do not want to do it use <ctrl-c>.

UPDATE: Recently I have also found out about <ctrl-\>. You can read more about it in this article:

What I didn’t know is that you can exit the shell by sending Ctrl-. The shell will exit immediately. As far as I know, it has the same effect as aborting the shell in the Break command, it doesn’t affect remote nodes and it also works outside of iex (for example, you can use to terminate your tests).

Szymon Jeż
  • 8,273
  • 4
  • 42
  • 60
  • 4
    There's also `:init.stop` which gracefully stops the erlang VM. – ibizaman Jul 10 '16 at 17:45
  • please improve your answer according to @raacer's structure and adopt his new information – flp Jan 23 '17 at 17:02
  • 1
    @flp Please see my comment here https://stackoverflow.com/questions/30085376/another-way-to-exiting-iex-other-than-ctrl-c/30095378#comment70835929_41253670 – Szymon Jeż Jan 24 '17 at 09:26
22
  1. Disconnect from shell and stop current node. This is what you need in most cases.

    1.1. Ctrl+\ - standard method to quit the Erlang shell. See "4.4 How do I quit the Erlang shell?" in Erlang -- Getting Started.

    1.2. Ctrl+C, a, Enter - via the (a)bort command of the Break menu.

    1.3. Ctrl+C, Ctrl+C - looks like undocumented feature of the Break menu.

    1.4. Ctrl+G, q, Enter - via the q (quit erlang) commant of the User Switch menu (see Erlang -- shell -- JCL Mode).

    Note: this leaves remote node alive if you have connected to it with iex --remsh (see iex --help and IEx -- Remote Shells).

  2. Shut down the node you are connected to.

    2.1. System.halt - quick and dirty shut down. The runtime system exits with status code 0 (clean exit without errors). You can also call System.halt(:abort) to abort with core dump. Same as :erlang.halt.

    2.2. :init.stop (System.stop in future versions) - clean shutdown. All applications are taken down smoothly, all code is unloaded, and all ports are closed before the system terminates by calling halt(Status).

    Note: this leaves your shell alive if you have connected to a remote shell with iex --remsh.

Notice that all these options are disabled if Erlang is started with the ignore break, +Bi, system flag: iex --erl +Bi (which can be useful, for example when running a restricted shell). See Erlang -- erl for more info.

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
raacer
  • 5,302
  • 3
  • 27
  • 46
  • 1
    Why did you feel compelled to restate answers that are already present? – Onorio Catenacci Dec 21 '16 at 13:09
  • 2
    @OnorioCatenacci Because the best answer is too complicated, does not have clean structure, and contains no links. – raacer Dec 21 '16 at 15:35
  • @OnorioCatenacci and btw, I think some explanations in the best answer are not really correct. – raacer Dec 21 '16 at 15:45
  • Hi, here is the author of "the best answer" :) I think that Your answer is now better than mine. I have up voted it. On the other hand @OnorioCatenacci is right. He is right that your answer contains mostly the same information like mine answer, and he was likely suggesting that you could just reformat my answer, fix some things and exited them than duplicating things. – Szymon Jeż Jan 24 '17 at 09:22
  • 1
    @flp mentioned that I should update my answer using the information and structure you have provided - I would be glad to do it, but I don't want to build on other people work, so I would feel better if You or another user did it :) – Szymon Jeż Jan 24 '17 at 09:22
  • @SzymonJeż Hi! Thanks for your vote. While I mostly agree, there is one thing to note. I was completely unsatisfied with your answer, not just with structure. So I have spent a lot of time for my own research and compiled my own answer from my point of view. There are some differences, probably minor, but I feel they are important. This is why I've created new answer. I think you wrote some wrong and non-accurate things, but I may be wrong too. There are many another questions that also have several similar answers. So I don't see any problem. Let people choose by voting. – raacer Jan 24 '17 at 14:55
8

Looks like

 System.halt

also works.

User314159
  • 7,733
  • 9
  • 39
  • 63
5

input in shell :c.q() and be happy

1

I always thought ctrl-G followed by q for quit was the official way to exit the iex shell.

See the IEx docs' section on the BREAK menu, which also shows how you can switch between alternate shells etc.

s3cur3
  • 2,749
  • 2
  • 27
  • 42
GavinBrelstaff
  • 3,016
  • 2
  • 21
  • 39
  • There is nothing about `q` on the specified page. Just a short note that the BREAK menu may be used to quit. But the menu itself also does not contain the `(q)uit` option, so this look completely undocumented for now. – raacer Dec 21 '16 at 01:08