@@ -27,7 +27,7 @@ a command in a sub-process::
27
27
use Symfony\Component\Process\Process;
28
28
use Symfony\Component\Process\Exception\ProcessFailedException;
29
29
30
- $process = new Process('ls -lsa');
30
+ $process = new Process(array( 'ls', ' -lsa') );
31
31
$process->run();
32
32
33
33
// executes after the command finishes
@@ -55,7 +55,7 @@ You can also use the :class:`Symfony\\Component\\Process\\Process` class with th
55
55
foreach construct to get the output while it is generated. By default, the loop waits
56
56
for new output before going to the next iteration::
57
57
58
- $process = new Process('ls -lsa');
58
+ $process = new Process(array( 'ls', ' -lsa') );
59
59
$process->start();
60
60
61
61
foreach ($process as $type => $data) {
@@ -72,7 +72,7 @@ for new output before going to the next iteration::
72
72
it is generated. That iterator is exposed via the ``getIterator() `` method
73
73
to allow customizing its behavior::
74
74
75
- $process = new Process('ls -lsa');
75
+ $process = new Process(array( 'ls', ' -lsa') );
76
76
$process->start();
77
77
$iterator = $process->getIterator($process::ITER_SKIP_ERR | $process::ITER_KEEP_OUTPUT);
78
78
foreach ($iterator as $data) {
@@ -90,7 +90,7 @@ with a non-zero code)::
90
90
use Symfony\Component\Process\Exception\ProcessFailedException;
91
91
use Symfony\Component\Process\Process;
92
92
93
- $process = new Process('ls -lsa');
93
+ $process = new Process(array( 'ls', ' -lsa') );
94
94
95
95
try {
96
96
$process->mustRun();
@@ -100,6 +100,38 @@ with a non-zero code)::
100
100
echo $exception->getMessage();
101
101
}
102
102
103
+ .. tip ::
104
+
105
+ .. versionadded :: 3.3
106
+ The ability to define commands as arrays of arguments was introduced in
107
+ Symfony 3.3.
108
+
109
+ Using array of arguments is the recommended way to define commands. This
110
+ saves you from any escaping and allows sending signals seamlessly
111
+ (e.g. to stop processes before completion.)::
112
+
113
+ $process = new Process(array('/path/command', '--flag', 'arg 1', 'etc.'));
114
+
115
+ If you need use stream redirections, conditional execution, or any other
116
+ features provided by the shell of your operating system, you can also define
117
+ commands as strings.
118
+
119
+ Please note that each OS provides a different syntax for their command-lines
120
+ so that it becomes your responsibility to deal with escaping and portability.
121
+
122
+ To provide any variable arguments to command-line string, pass them as
123
+ environment variables using the second argument of the ``run() ``,
124
+ ``mustRun() `` or ``start() `` methods. Referencing them is also OS-dependent::
125
+
126
+ // On Unix-like OSes (Linux, macOS)
127
+ $process = new Process('echo "$MESSAGE"');
128
+
129
+ // On Windows
130
+ $process = new Process('echo "!MESSAGE!"');
131
+
132
+ // On both Unix-like and Windows
133
+ $process->run(null, array('MESSAGE' => 'Something to output'));
134
+
103
135
Getting real-time Process Output
104
136
--------------------------------
105
137
@@ -110,7 +142,7 @@ anonymous function to the
110
142
111
143
use Symfony\Component\Process\Process;
112
144
113
- $process = new Process('ls -lsa');
145
+ $process = new Process(array( 'ls', ' -lsa') );
114
146
$process->run(function ($type, $buffer) {
115
147
if (Process::ERR === $type) {
116
148
echo 'ERR > '.$buffer;
@@ -129,7 +161,7 @@ process, the :method:`Symfony\\Component\\Process\\Process::isRunning` method
129
161
to check if the process is done and the
130
162
:method: `Symfony\\ Component\\ Process\\ Process::getOutput ` method to get the output::
131
163
132
- $process = new Process('ls -lsa');
164
+ $process = new Process(array( 'ls', ' -lsa') );
133
165
$process->start();
134
166
135
167
while ($process->isRunning()) {
@@ -141,7 +173,7 @@ to check if the process is done and the
141
173
You can also wait for a process to end if you started it asynchronously and
142
174
are done doing other stuff::
143
175
144
- $process = new Process('ls -lsa');
176
+ $process = new Process(array( 'ls', ' -lsa') );
145
177
$process->start();
146
178
147
179
// ... do other things
@@ -180,7 +212,7 @@ are done doing other stuff::
180
212
a callback that is called repeatedly whilst the process is still running, passing
181
213
in the output and its type::
182
214
183
- $process = new Process('ls -lsa');
215
+ $process = new Process(array( 'ls', ' -lsa') );
184
216
$process->start();
185
217
186
218
$process->wait(function ($type, $buffer) {
@@ -199,7 +231,7 @@ Before a process is started, you can specify its standard input using either the
199
231
of the constructor. The provided input can be a string, a stream resource or a
200
232
Traversable object::
201
233
202
- $process = new Process('cat');
234
+ $process = new Process('cat'] );
203
235
$process->setInput('foobar');
204
236
$process->run();
205
237
@@ -212,7 +244,7 @@ provides the :class:`Symfony\\Component\\Process\\InputStream` class::
212
244
$input = new InputStream();
213
245
$input->write('foo');
214
246
215
- $process = new Process('cat');
247
+ $process = new Process(array( 'cat') );
216
248
$process->setInput($input);
217
249
$process->start();
218
250
@@ -238,7 +270,7 @@ The input of a process can also be defined using `PHP streams`_::
238
270
239
271
$stream = fopen('php://temporary', 'w+');
240
272
241
- $process = new Process('cat');
273
+ $process = new Process(array( 'cat') );
242
274
$process->setInput($stream);
243
275
$process->start();
244
276
@@ -264,7 +296,7 @@ is sent to the running process. The default signal sent to a process is ``SIGKIL
264
296
Please read the :ref: `signal documentation below<reference-process-signal> `
265
297
to find out more about signal handling in the Process component::
266
298
267
- $process = new Process('ls -lsa');
299
+ $process = new Process(array( 'ls', ' -lsa') );
268
300
$process->start();
269
301
270
302
// ... do other things
@@ -285,38 +317,6 @@ instead::
285
317
);
286
318
$process->run();
287
319
288
- To make your code work better on all platforms, you might want to use the
289
- :class: `Symfony\\ Component\\ Process\\ ProcessBuilder ` class instead::
290
-
291
- use Symfony\Component\Process\ProcessBuilder;
292
-
293
- $processBuilder = new ProcessBuilder(array('ls', '-lsa'));
294
- $processBuilder->getProcess()->run();
295
-
296
- In case you are building a binary driver, you can use the
297
- :method: `Symfony\\ Component\\ Process\\ ProcessBuilder::setPrefix ` method to prefix all
298
- the generated process commands.
299
-
300
- The following example will generate two process commands for a tar binary
301
- adapter::
302
-
303
- use Symfony\Component\Process\ProcessBuilder;
304
-
305
- $processBuilder = new ProcessBuilder();
306
- $processBuilder->setPrefix('/usr/bin/tar');
307
-
308
- // '/usr/bin/tar' '--list' '--file=archive.tar.gz'
309
- echo $processBuilder
310
- ->setArguments(array('--list', '--file=archive.tar.gz'))
311
- ->getProcess()
312
- ->getCommandLine();
313
-
314
- // '/usr/bin/tar' '-xzf' 'archive.tar.gz'
315
- echo $processBuilder
316
- ->setArguments(array('-xzf', 'archive.tar.gz'))
317
- ->getProcess()
318
- ->getCommandLine();
319
-
320
320
Process Timeout
321
321
---------------
322
322
@@ -325,7 +325,7 @@ timeout (in seconds)::
325
325
326
326
use Symfony\Component\Process\Process;
327
327
328
- $process = new Process('ls -lsa');
328
+ $process = new Process(array( 'ls', ' -lsa') );
329
329
$process->setTimeout(3600);
330
330
$process->run();
331
331
@@ -357,7 +357,7 @@ considers the time since the last output was produced by the process::
357
357
358
358
use Symfony\Component\Process\Process;
359
359
360
- $process = new Process('something-with-variable-runtime');
360
+ $process = new Process(array( 'something-with-variable-runtime') );
361
361
$process->setTimeout(3600);
362
362
$process->setIdleTimeout(60);
363
363
$process->run();
@@ -373,21 +373,12 @@ When running a program asynchronously, you can send it POSIX signals with the
373
373
374
374
use Symfony\Component\Process\Process;
375
375
376
- $process = new Process('find / -name " rabbit"' );
376
+ $process = new Process(array( 'find', '/', ' -name', ' rabbit') );
377
377
$process->start();
378
378
379
379
// will send a SIGKILL to the process
380
380
$process->signal(SIGKILL);
381
381
382
- .. caution ::
383
-
384
- Due to some limitations in PHP, if you're using signals with the Process
385
- component, you may have to prefix your commands with `exec `_. Please read
386
- `Symfony Issue#5759 `_ and `PHP Bug#39992 `_ to understand why this is happening.
387
-
388
- POSIX signals are not available on Windows platforms, please refer to the
389
- `PHP documentation `_ for available signals.
390
-
391
382
Process Pid
392
383
-----------
393
384
@@ -396,17 +387,11 @@ You can access the `pid`_ of a running process with the
396
387
397
388
use Symfony\Component\Process\Process;
398
389
399
- $process = new Process('/usr/bin/php worker.php');
390
+ $process = new Process(array( '/usr/bin/php', ' worker.php') );
400
391
$process->start();
401
392
402
393
$pid = $process->getPid();
403
394
404
- .. caution ::
405
-
406
- Due to some limitations in PHP, if you want to get the pid of a symfony Process,
407
- you may have to prefix your commands with `exec `_. Please read
408
- `Symfony Issue#5759 `_ to understand why this is happening.
409
-
410
395
Disabling Output
411
396
----------------
412
397
@@ -417,7 +402,7 @@ Use :method:`Symfony\\Component\\Process\\Process::disableOutput` and
417
402
418
403
use Symfony\Component\Process\Process;
419
404
420
- $process = new Process('/usr/bin/php worker.php');
405
+ $process = new Process(array( '/usr/bin/php', ' worker.php') );
421
406
$process->disableOutput();
422
407
$process->run();
423
408
@@ -449,9 +434,6 @@ absolute path of the executable PHP binary available on your server::
449
434
$phpBinaryPath = $phpBinaryFinder->find();
450
435
// $phpBinaryPath = '/usr/local/bin/php' (the result will be different on your computer)
451
436
452
- .. _`Symfony Issue#5759` : https://github.com/symfony/symfony/issues/5759
453
- .. _`PHP Bug#39992` : https://bugs.php.net/bug.php?id=39992
454
- .. _`exec` : https://en.wikipedia.org/wiki/Exec_(operating_system)
455
437
.. _`pid` : https://en.wikipedia.org/wiki/Process_identifier
456
438
.. _`PHP Documentation` : https://php.net/manual/en/pcntl.constants.php
457
439
.. _Packagist : https://packagist.org/packages/symfony/process
0 commit comments