.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "SyncExec 3pm" .TH SyncExec 3pm "2016-12-17" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Proc::SyncExec \- Spawn processes but report exec() errors .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& # Normal\-looking piped opens which properly report exec() errors in $!: \& sync_open WRITER_FH, "|command \-with args" or die $!; \& sync_open READER_FH, "command \-with args|" or die $!; \& \& # Synchronized fork/exec which reports exec errors in $!: \& $pid = sync_exec $command, @arg; \& $pid = sync_exec $code_ref, $cmd, @arg; # run code after fork in kid \& \& # fork() which retries if it fails, then croaks() if it still fails. \& $pid = fork_retry; \& $pid = fork_retry 100; # retry 100 times rather than 5 \& $pid = fork_retry 100, 2; # sleep 2 rather than 5 seconds between \& \& # A couple of interfaces similar to sync_open() but which let you \& # avoid the shell: \& $pid = sync_fhpopen_noshell READERFH, \*(Aqr\*(Aq, @command; \& $pid = sync_fhpopen_noshell WRITERFH, \*(Aqw\*(Aq, @command; \& $fh = sync_popen_noshell \*(Aqr\*(Aq, @command_which_outputs; \& $fh = sync_popen_noshell \*(Aqw\*(Aq, @command_which_inputs; \& ($fh, $pid) = sync_popen_noshell \*(Aqr\*(Aq, @command_which_outputs; \& ($fh, $pid)= sync_popen_noshell \*(Aqw\*(Aq, @command_which_inputs; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module contains functions for synchronized process spawning with full error return. If the child's \fIexec()\fR call fails the reason for the failure is reported back to the parent. .PP These functions will \fIcroak()\fR if they encounter an unexpected system error, such as a \fIpipe()\fR failure or a repeated \fIfork()\fR failure. .PP Nothing is exported by default. .IP "\fBfork_retry\fR [\fImax-retries\fR [\fIsleep-between\fR]]" 4 .IX Item "fork_retry [max-retries [sleep-between]]" This function runs \fIfork()\fR until it succeeds or until \fImax-retries\fR (default 5) attempts have been made, sleeping \fIsleep-between\fR seconds (default 5) between attempts. If the last \fIfork()\fR fails \fBfork_retry\fR \&\fIcroak()\fRs. .IP "\fBsync_exec\fR [\fIcode\fR] \fIcommand\fR..." 4 .IX Item "sync_exec [code] command..." This function is similar to a \fIfork()\fR/\fIexec()\fR sequence but with a few twists. .Sp \&\fBsync_exec\fR does not return until after the \fIfork()\fRed child has already performed its \fIexec()\fR. The synchronization this provides is useful in some unusual circumstances. .Sp Normally the pid of the child process is returned. However, if the child fails its \fIexec()\fR \fBsync_exec\fR returns undef and sets $! to the reason for the child's \fIexec()\fR failure. .Sp Since the \f(CW@cmd\fR array is passed directly to Perl's \fIexec()\fR Perl might choose to invoke the command via the shell if \f(CW@cmd\fR contains only one element and it looks like it needs a shell to interpret it. If this happens the return value of \fBsync_exec\fR only indicates whether the \&\fIexec()\fR of the shell worked. .Sp The optional initial \fIcode\fR argument must be a code reference. If it is present it is run in the child just before \fIexec()\fR is called. You can use this to set up redirections or whatever. If \fIcode\fR returns false no exec is performed, instead a failure is returned using the current $! value (or \s-1EINTR\s0 if $! is 0). .Sp If the \fIfork()\fR fails or if there is some other unexpected system error \&\fBsync_exec\fR \fIcroak()\fRs rather than returning. .IP "\fBsync_fhpopen_noshell\fR \fIfh\fR \fItype\fR \fIcmd\fR [\fIarg\fR]..." 4 .IX Item "sync_fhpopen_noshell fh type cmd [arg]..." This is a \fIpopen()\fR but it never invokes the shell and it uses \fIsync_exec()\fR under the covers. See \*(L"sync_exec\*(R". .Sp The \fItype\fR is either \f(CW\*(Aqr\*(Aq\fR to read from the process or \f(CW\*(Aqw\*(Aq\fR to write to it. .Sp The return value is the pid of the forked process. .IP "\fBsync_popen_noshell\fR \fItype\fR \fIcmd\fR \fIarg\fR..." 4 .IX Item "sync_popen_noshell type cmd arg..." This is like \fBsync_fhpopen_noshell\fR, but you don't have to supply the filehandle. .Sp If called in an array context the return value is a list consisting of the filehandle and the \s-1PID\s0 of the child. In a scalar context only the filehandle is returned. .IP "\fBsync_open\fR \fIfh\fR [\fIopen-spec\fR]" 4 .IX Item "sync_open fh [open-spec]" This is like a Perl \fIopen()\fR except that if a pipe is involved and the implied \fIexec()\fR fails \fIsync_open()\fR fails with $! set appropriately. See \&\*(L"sync_exec\*(R". .Sp Like \fBsync_exec\fR, \fBsync_open\fR \fIcroak()\fRs if there is an unexpected system error (such as a failed \fIpipe()\fR). .Sp Also like \fBsync_exec\fR, if you use a command which Perl needs to use the shell to interpret you'll only know if the exec of the shell worked. Use \fBsync_fhpopen_noshell\fR or \fBsync_exec\fR to be sure that this doesn't happen. .SH "AUTHOR" .IX Header "AUTHOR" Roderick Schertler <\fIroderick@argon.org\fR> .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fIperl\fR\|(1).