@@ -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([ '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([ '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([ '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([ 'ls', ' -lsa'] );
94
94
95
95
try {
96
96
$process->mustRun();
@@ -100,6 +100,33 @@ 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 array of arguments was introduced in Symfony 3.3.
107
+
108
+ When defining commands as arrays, its arguments are escaped for you and you should not
109
+ escape them any further. You can also define commands as strings. When doing so, the
110
+ command-line is parsed by the underlying shell of your operating system. This allows
111
+ using e.g. stream redirections or conditional execution, but this is a lot less portable
112
+ since each OS (esp. Windows vs Unix-like) deals with escaping and parsing differently.
113
+ When passing a command-line string, it becomes your responsibility to deal with this.
114
+
115
+ Using array of arguments is the recommended way to define commands.
116
+
117
+ If you need to define commands as strings, variable arguments should be passed as
118
+ environment variables using the second argument of the ``run() ``, ``mustRun() `` or
119
+ ``start() `` methods. Referencing them in command-line strings is OS-dependent::
120
+
121
+ // On Unix-like OSes
122
+ $process = new Process('echo "$MESSAGE"');
123
+
124
+ // On Windows
125
+ $process = new Process('echo %MESSAGE%');
126
+
127
+ // On both Unix-like and Windows
128
+ $process->run(null, ['MESSAGE' => 'Something to output']);
129
+
103
130
Getting real-time Process Output
104
131
--------------------------------
105
132
@@ -110,7 +137,7 @@ anonymous function to the
110
137
111
138
use Symfony\Component\Process\Process;
112
139
113
- $process = new Process('ls -lsa');
140
+ $process = new Process([ 'ls', ' -lsa'] );
114
141
$process->run(function ($type, $buffer) {
115
142
if (Process::ERR === $type) {
116
143
echo 'ERR > '.$buffer;
@@ -129,7 +156,7 @@ process, the :method:`Symfony\\Component\\Process\\Process::isRunning` method
129
156
to check if the process is done and the
130
157
:method: `Symfony\\ Component\\ Process\\ Process::getOutput ` method to get the output::
131
158
132
- $process = new Process('ls -lsa');
159
+ $process = new Process([ 'ls', ' -lsa'] );
133
160
$process->start();
134
161
135
162
while ($process->isRunning()) {
@@ -141,7 +168,7 @@ to check if the process is done and the
141
168
You can also wait for a process to end if you started it asynchronously and
142
169
are done doing other stuff::
143
170
144
- $process = new Process('ls -lsa');
171
+ $process = new Process([ 'ls', ' -lsa'] );
145
172
$process->start();
146
173
147
174
// ... do other things
@@ -180,7 +207,7 @@ are done doing other stuff::
180
207
a callback that is called repeatedly whilst the process is still running, passing
181
208
in the output and its type::
182
209
183
- $process = new Process('ls -lsa');
210
+ $process = new Process([ 'ls', ' -lsa'] );
184
211
$process->start();
185
212
186
213
$process->wait(function ($type, $buffer) {
@@ -199,7 +226,7 @@ Before a process is started, you can specify its standard input using either the
199
226
of the constructor. The provided input can be a string, a stream resource or a
200
227
Traversable object::
201
228
202
- $process = new Process('cat');
229
+ $process = new Process([ 'cat'] );
203
230
$process->setInput('foobar');
204
231
$process->run();
205
232
@@ -212,7 +239,7 @@ provides the :class:`Symfony\\Component\\Process\\InputStream` class::
212
239
$input = new InputStream();
213
240
$input->write('foo');
214
241
215
- $process = new Process('cat');
242
+ $process = new Process([ 'cat'] );
216
243
$process->setInput($input);
217
244
$process->start();
218
245
@@ -238,7 +265,7 @@ The input of a process can also be defined using `PHP streams`_::
238
265
239
266
$stream = fopen('php://temporary', 'w+');
240
267
241
- $process = new Process('cat');
268
+ $process = new Process([ 'cat'] );
242
269
$process->setInput($stream);
243
270
$process->start();
244
271
@@ -264,7 +291,7 @@ is sent to the running process. The default signal sent to a process is ``SIGKIL
264
291
Please read the :ref: `signal documentation below<reference-process-signal> `
265
292
to find out more about signal handling in the Process component::
266
293
267
- $process = new Process('ls -lsa');
294
+ $process = new Process([ 'ls', ' -lsa'] );
268
295
$process->start();
269
296
270
297
// ... do other things
@@ -285,38 +312,6 @@ instead::
285
312
);
286
313
$process->run();
287
314
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
315
Process Timeout
321
316
---------------
322
317
@@ -325,7 +320,7 @@ timeout (in seconds)::
325
320
326
321
use Symfony\Component\Process\Process;
327
322
328
- $process = new Process('ls -lsa');
323
+ $process = new Process([ 'ls', ' -lsa'] );
329
324
$process->setTimeout(3600);
330
325
$process->run();
331
326
@@ -357,7 +352,7 @@ considers the time since the last output was produced by the process::
357
352
358
353
use Symfony\Component\Process\Process;
359
354
360
- $process = new Process('something-with-variable-runtime');
355
+ $process = new Process([ 'something-with-variable-runtime'] );
361
356
$process->setTimeout(3600);
362
357
$process->setIdleTimeout(60);
363
358
$process->run();
@@ -373,21 +368,12 @@ When running a program asynchronously, you can send it POSIX signals with the
373
368
374
369
use Symfony\Component\Process\Process;
375
370
376
- $process = new Process('find / -name " rabbit"' );
371
+ $process = new Process([ 'find', '/', ' -name', ' rabbit'] );
377
372
$process->start();
378
373
379
374
// will send a SIGKILL to the process
380
375
$process->signal(SIGKILL);
381
376
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
377
Process Pid
392
378
-----------
393
379
@@ -396,17 +382,11 @@ You can access the `pid`_ of a running process with the
396
382
397
383
use Symfony\Component\Process\Process;
398
384
399
- $process = new Process('/usr/bin/php worker.php');
385
+ $process = new Process([ '/usr/bin/php', ' worker.php'] );
400
386
$process->start();
401
387
402
388
$pid = $process->getPid();
403
389
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
390
Disabling Output
411
391
----------------
412
392
@@ -417,7 +397,7 @@ Use :method:`Symfony\\Component\\Process\\Process::disableOutput` and
417
397
418
398
use Symfony\Component\Process\Process;
419
399
420
- $process = new Process('/usr/bin/php worker.php');
400
+ $process = new Process([ '/usr/bin/php', ' worker.php'] );
421
401
$process->disableOutput();
422
402
$process->run();
423
403
@@ -449,9 +429,6 @@ absolute path of the executable PHP binary available on your server::
449
429
$phpBinaryPath = $phpBinaryFinder->find();
450
430
// $phpBinaryPath = '/usr/local/bin/php' (the result will be different on your computer)
451
431
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
432
.. _`pid` : https://en.wikipedia.org/wiki/Process_identifier
456
433
.. _`PHP Documentation` : https://php.net/manual/en/pcntl.constants.php
457
434
.. _Packagist : https://packagist.org/packages/symfony/process
0 commit comments