> /dev...">

Perl freezes when exiting (after closing file descriptor)

I have a function that (in short):

my $file = IO::File->new("| some_command >> /dev/null 2>&1") 
    or die "cannot open some_command for writing: $!\n";
...
undef $file;

Right now I'm not even writing anything for $file. Currently, there are no other operations at $fileall. When I run the program, it does not exit properly. I see that the handle is closed, but my program is still waiting for the process to complete. Captured with strace:

close(6)                                = 0
rt_sigaction(SIGHUP, {SIG_IGN}, {SIG_DFL}, 8) = 0
rt_sigaction(SIGINT, {SIG_IGN}, {SIG_DFL}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_IGN}, {SIG_DFL}, 8) = 0
wait4(16861, ^C <unfinished ...>

I do not see this problem if I open the same process for reading.

What do I need to do for the program to exit?

Edit: The suggestions so far have been to use the Expect library or to complete the input stream using ctrl + d. But at the moment I do not want to interact with the program. I want him to finish right now, without any I / O. Is it possible?

+3
4

undef $file . $file , , , IO::File. $file , , . $file->close .


, , .

my ($file,$pid);
$pid = open($file, "| some_command >> /dev/null 2>&1");

kill 'TERM',$pid;

. , IO::File::new, .

+9

some_command , , , , , .

, , , - , $file- > close() / .

: D? , some_command tty stdin, passwd. , .

D , , .

$file- > close() undef?

+6

some_command ? , grep? ? , ... chfn? - ? , ?

, Expect, .

0

, , some_command init, perl. Perl , - yay UNIX.

my $file = IO::File->new("| some_command >> /dev/null 2>&1 &")

: &, some_command , .

    or die "cannot open some_command for writing: $!\n";  # now useless

If I some_commandquit as soon as he got the EOF on stdin (and never stops reading from stdin), I would expect it to not be needed.

$ cat | some_command
^ D

Does it freeze and can you fix it?

0
source

Source: https://habr.com/ru/post/1720300/


All Articles