1SyncExec(3) User Contributed Perl Documentation SyncExec(3)
2
3
4
6 Proc::SyncExec - Spawn processes but report exec() errors
7
9 # Normal-looking piped opens which properly report exec() errors in $!:
10 sync_open WRITER_FH, "|command -with args" or die $!;
11 sync_open READER_FH, "command -with args|" or die $!;
12
13 # Synchronized fork/exec which reports exec errors in $!:
14 $pid = sync_exec $command, @arg;
15 $pid = sync_exec $code_ref, $cmd, @arg; # run code after fork in kid
16
17 # fork() which retries if it fails, then croaks() if it still fails.
18 $pid = fork_retry;
19 $pid = fork_retry 100; # retry 100 times rather than 5
20 $pid = fork_retry 100, 2; # sleep 2 rather than 5 seconds between
21
22 # A couple of interfaces similar to sync_open() but which let you
23 # avoid the shell:
24 $pid = sync_fhpopen_noshell READERFH, 'r', @command;
25 $pid = sync_fhpopen_noshell WRITERFH, 'w', @command;
26 $fh = sync_popen_noshell 'r', @command_which_outputs;
27 $fh = sync_popen_noshell 'w', @command_which_inputs;
28 ($fh, $pid) = sync_popen_noshell 'r', @command_which_outputs;
29 ($fh, $pid)= sync_popen_noshell 'w', @command_which_inputs;
30
32 This module contains functions for synchronized process spawning with
33 full error return. If the child's exec() call fails the reason for the
34 failure is reported back to the parent.
35
36 These functions will croak() if they encounter an unexpected system
37 error, such as a pipe() failure or a repeated fork() failure.
38
39 Nothing is exported by default.
40
41 fork_retry [max-retries [sleep-between]]
42 This function runs fork() until it succeeds or until max-retries
43 (default 5) attempts have been made, sleeping sleep-between seconds
44 (default 5) between attempts. If the last fork() fails fork_retry
45 croak()s.
46
47 sync_exec [code] command...
48 This function is similar to a fork()/exec() sequence but with a few
49 twists.
50
51 sync_exec does not return until after the fork()ed child has
52 already performed its exec(). The synchronization this provides is
53 useful in some unusual circumstances.
54
55 Normally the pid of the child process is returned. However, if the
56 child fails its exec() sync_exec returns undef and sets $! to the
57 reason for the child's exec() failure.
58
59 Since the @cmd array is passed directly to Perl's exec() Perl might
60 choose to invoke the command via the shell if @cmd contains only
61 one element and it looks like it needs a shell to interpret it. If
62 this happens the return value of sync_exec only indicates whether
63 the exec() of the shell worked.
64
65 The optional initial code argument must be a code reference. If it
66 is present it is run in the child just before exec() is called.
67 You can use this to set up redirections or whatever. If code
68 returns false no exec is performed, instead a failure is returned
69 using the current $! value (or EINTR if $! is 0).
70
71 If the fork() fails or if there is some other unexpected system
72 error sync_exec croak()s rather than returning.
73
74 sync_fhpopen_noshell fh type cmd [arg]...
75 This is a popen() but it never invokes the shell and it uses
76 sync_exec() under the covers. See "sync_exec".
77
78 The type is either 'r' to read from the process or 'w' to write to
79 it.
80
81 The return value is the pid of the forked process.
82
83 sync_popen_noshell type cmd arg...
84 This is like sync_fhpopen_noshell, but you don't have to supply the
85 filehandle.
86
87 If called in an array context the return value is a list consisting
88 of the filehandle and the PID of the child. In a scalar context
89 only the filehandle is returned.
90
91 sync_open fh [open-spec]
92 This is like a Perl open() except that if a pipe is involved and
93 the implied exec() fails sync_open() fails with $! set
94 appropriately. See "sync_exec".
95
96 Like sync_exec, sync_open croak()s if there is an unexpected system
97 error (such as a failed pipe()).
98
99 Also like sync_exec, if you use a command which Perl needs to use
100 the shell to interpret you'll only know if the exec of the shell
101 worked. Use sync_fhpopen_noshell or sync_exec to be sure that this
102 doesn't happen.
103
105 Roderick Schertler <roderick@argon.org>
106
108 perl(1).
109
110
111
112perl v5.28.0 2005-02-04 SyncExec(3)