DEvil0000

DEvil0000

Linking a port / exit normal in case of kill -X <os_pid>

Hi there,
I want to open a os process from elixir and get informed in case it crashes. I tried it with port and link but when I kill the external os process the exit always reports normal.
I was expecting to get something else then normal when it exits abnormal like in kill -9 or kill -15.

Since I am actually using elixir 1.5 my test code looks like:

       :erlang.process_flag(:trap_exit, true)
        port = Port.open({:spawn, "sleep 360"}, [:exit_status])
        Process.link port
        :erlang.monitor :port, port

And I get the following:

{#Port<0.6363>, {:exit_status, 137}}
{:EXIT, #Port<0.6363>, :normal}
{:DOWN, #Reference<0.3242658756.1180172289.143115>, :port, #Port<0.6363>, :normal}

The only thing I could use as a workaround is the exit status as it looks like.

Does one of you have a good idea how to get link working for this case?

Is this a bug or a feature?

Communication with os processes, killing them or sending signals is quite tricky anyway in erlang…

Most Liked

grych

grych

Creator of Drab

Everything works as expected.

When you kill the process in unix, its exit status is set to non-zero value, depending on the signal you’ve sent (128 + signal number, so in your case it is 128+9=137).

OvermindDL1

OvermindDL1

It’s not possible to know it segfaulted, and a return code from an application may not actually be an error but could be returning valid information that is actually wanted (this is done quite commonly), only the user can know.

grych

grych

Creator of Drab

I think you are missing the difference between OS and Erlang processes.

This is an exit of the erlang process, not the unix one. Port.open creates an erlang process, which runs the external command and waits for it to end. From the erlang point of view, the port process finished normally - it did the job.

DEvil0000

DEvil0000

I was expecting those cases would also get reflected in the exit beeing not normal so link would make sense.

grych

    March 12

Everything works as expected.

DEvil0000:
{#Port<0.6363>, {:exit_status, 137}}

When you kill the process in unix, its exit status is set to non-zero value, depending on the signal you’ve sent (128 + signal number, so in your case it is 128+9=137).

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement