fxn

fxn

I have a script executed with mix run that has an infinite loop that collects some data in an agent. I’d like to print that data when I interrupt the script.

Tried System.at_exit/1 without success, maybe because the only way I know to interrupt the execution is C-c C-c.

Is there a way to accomplish that?

Showing Posts 1 to 10

NobbZ

NobbZ

Ctrl+C, Ctrl+C kills the Virtual Machine. Ctrl+G, Q should exit in a way that the exit handler gets called, alternatively you can try kill $BEAM_PID from another terminal.

fxn

fxn OP

I am trying

System.at_exit(fn _status ->
  IO.puts("Bye!")
end)

IO.puts("Interrupt me!")
Process.sleep(:infinity)

C-g + q does not seem to interrupt the script here, and kill does, but does not trigger System.at_exit/1.

rvirding

rvirding

Creator of Erlang

That is weird. What output do you get when you do Ctrl-G? It should be something like

User switch command
 --> 

Try doing ?

Btw you did press Enter after the q? It is not stated anywhere but otherwise nothing happens.

fxn

fxn OP

When I type Ctrl-G nothing happens apparently, I have recorded a GIF.

CleanShot 2020-06-14 at 18.54.59

Is that strange? This is macOS and elixir -v returns

Erlang/OTP 23 [erts-11.0.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe]

Elixir 1.10.3 (compiled with Erlang/OTP 23)

They two are installed with asdf.

axelson

axelson

Scenic Core Team

Just throwing out a random idea, but is it different if you execute the script with mix run?

fxn

fxn OP

With mix run happens the same, indeed I was trying elixir to remove Mix from the equation.

I have revised shortcuts in iTerm2 in case C-g is intercepted, but don’t find anything. Terminal.app makes no difference either.

C-g q shows “User switch command” in IEx and erl, which to me suggests the character is actually not intercepted by my terminals or my OS. However, System.at_exit/1 does not seem to be invoked:

iex(1)> System.at_exit(fn _ -> IO.puts("Bye!") end)
:ok
iex(2)>
User switch command
 --> q
~/tmp/x %

Also, I have replaced the sleep call with a dummy receive block, just in case sleeping meant whatever code needs to be triggered by C-g is just within the slept process. No luck either.

Does

System.at_exit(fn _status ->
  IO.puts("Bye!")
end)

IO.puts("Interrupt me!")
Process.sleep(:infinity)

print “Bye!” in your machines using C-g q?

fxn

fxn OP

Ah, I also tried in a Docker container that runs Linux, same.

ChrisYammine

ChrisYammine

This may be of interest to you: https://stackoverflow.com/questions/42324276/catching-exit-signals-in-elixir-escript

AFAIK erts will trap the TERM signal and call init:stop, but when you’re C-c ing that’s an INT signal. You should be able to achieve your desired behavior with a bash wrapper like in the linked stack overflow question.

fxn

fxn OP

However, several people in this thread say C-g q should work. Note that C-g does not send a signal, you get the control character 7. If that is possible, a shell script wrapper would be overkill.

I just need to interrupt a script in an ordered way. I have an at_exit callback, but could be taking down a supervision tree orderly or whatever.

rvirding

rvirding

Creator of Erlang

A thing to realise here is that the handling of Ctrl-G and its commands only work when you are running a shell. Without a shell this interface is not generally not started. You can see this if you look in the elixir shell script and see that when an iex shell is not start then there is a -noshell argument to the resultant erl command. Doing elixir foo.exs does not start a shell and, hence, no handling of Ctrl-G and q.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews