[erlang-questions] open_port + spawn and closing the spawned application when port closes

Per Hedeland per@REDACTED
Wed Dec 19 15:43:02 CET 2018


On 2018-12-19 14:11, Fred Hebert wrote:
> On 12/13, Vans S wrote:
>> I recall there was a way to do this by basho but wondering if there are new alternatives recently.
>>
>> Basically the example is a simple one, open_port({spawn, "curl -L "http://releases.ubuntu.com/18.04.1.0/ubuntu-18.04.1.0-live-server-amd64.iso?" --progress-bar -o /dev/null"}) now kill the process, 
>> close the port, or kill the emulator.  The curl process keeps running.  I believe it was when the port closes, stdin/stdout closes for the process, and the work around was to wrap the process in a 
>> bash script that would detect stdin/out closed, and terminate the process it wrapped.
>>
>> Any thoughts on this, or perhaps help finding that bash script again?
> 
> The safest option for a flexible case is https://github.com/saleyn/erlexec

Erlexec is of course infinitely more flexible, but in case EOF -> kill
"translation" is all you want, I believe a correctly written wrapper
script is equally safe. So, to Vans, you will find both a bash script and a
slightly improved/fixed version that doesn't specifically require bash at:

http://erlang.org/pipermail/erlang-questions/2016-November/090901.html

> You might have also thought of just using something like `cat | <command>' which would try to stop and shut things down when stdin closes, but erlexec is a safer bet.

Well, yes, without a *very* liberal interpretation of "something like",
that solution doesn't work at all - there is nothing in it that "would
try to stop and shut things down". 'cat' will terminate when it sees EOF
on its stdin, and as a consequence <command> will see EOF on *its* stdin
- just like it would have done if the 'cat |' part wasn't there at all -
and that's all that will happen.

--Per



More information about the erlang-questions mailing list