Error spawning php: command failed with exit code 1

Access denied error while trying to build Android app bundle

Mon, 14 Jan 2019

This error comes up when you don’t have full permissions to the project folder. You want to run the chmod command in order to give your folder proper permissions.

Try running this command first in your terminal

chmod -R a+rwx project/

If that doesn’t work try using 777 instead like this

chmod -R 777 project/

[PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8]

proc_open Execute a command and open file pointers for input/output

Description

proc_open[
    array|string $command,
    array $descriptor_spec,
    array &$pipes,
    ?string $cwd = null,
    ?array $env_vars = null,
    ?array $options = null
]: resource|false

Parameters

command

The commandline to execute as string. Special characters have to be properly escaped, and proper quoting has to be applied.

Note: On Windows, unless bypass_shell is set to true in options, the command is passed to cmd.exe [actually, %ComSpec%] with the /c flag as unquoted string [i.e. exactly as has been given to proc_open[]]. This can cause cmd.exe to remove enclosing quotes from command [for details see the cmd.exe documentation], resulting in unexpected, and potentially even dangerous behavior, because cmd.exe error messages may contain [parts of] the passed command [see example below].

As of PHP 7.4.0, command may be passed as array of command parameters. In this case the process will be opened directly [without going through a shell] and PHP will take care of any necessary argument escaping.

Note:

On Windows, the argument escaping of the array elements assumes that the command line parsing of the executed command is compatible with the parsing of command line arguments done by the VC runtime.

descriptor_spec

An indexed array where the key represents the descriptor number and the value represents how PHP will pass that descriptor to the child process. 0 is stdin, 1 is stdout, while 2 is stderr.

Each element can be:

  • An array describing the pipe to pass to the process. The first element is the descriptor type and the second element is an option for the given type. Valid types are pipe [the second element is either r to pass the read end of the pipe to the process, or w to pass the write end] and file [the second element is a filename]. Note that anything else than w is treated like r.
  • A stream resource representing a real file descriptor [e.g. opened file, a socket, STDIN].

The file descriptor numbers are not limited to 0, 1 and 2 - you may specify any valid file descriptor number and it will be passed to the child process. This allows your script to interoperate with other scripts that run as "co-processes". In particular, this is useful for passing passphrases to programs like PGP, GPG and openssl in a more secure manner. It is also useful for reading status information provided by those programs on auxiliary file descriptors.

pipes

Will be set to an indexed array of file pointers that correspond to PHP's end of any pipes that are created.

cwd

The initial working dir for the command. This must be an absolute directory path, or null if you want to use the default value [the working dir of the current PHP process]

env_vars

An array with the environment variables for the command that will be run, or null to use the same environment as the current PHP process

options

Allows you to specify additional options. Currently supported options include:

  • suppress_errors [windows only]: suppresses errors generated by this function when it's set to true
  • bypass_shell [windows only]: bypass cmd.exe shell when set to true
  • blocking_pipes [windows only]: force blocking pipes when set to true
  • create_process_group [windows only]: allow the child process to handle CTRL events when set to true
  • create_new_console [windows only]: the new process has a new console, instead of inheriting its parent's console

Return Values

Returns a resource representing the process, which should be freed using proc_close[] when you are finished with it. On failure returns false.

Changelog

VersionDescription
7.4.4 Added the create_new_console option to the options parameter.
7.4.0 proc_open[] now also accepts an array for the command.
7.4.0 Added the create_process_group option to the options parameter.

Examples

Example #1 A proc_open[] example

The above example will output something similar to:

Array
[
    [some_option] => aeiou
    [PWD] => /tmp
    [SHLVL] => 1
    [_] => /usr/local/bin/php
]
command returned 0

Example #2 proc_open[] quirk on Windows

While one may expect the following program to search the file filename.txt for the text search and to print the results, it behaves rather differently.

The above example will output:

'findstr" "search" "filename.txt' is not recognized as an internal or external command,
operable program or batch file.

To work around that behavior, it is usually sufficient to enclose the command in additional quotes:

$cmd = '""findstr" "search" "filename.txt""';

Notes

Note:

Windows compatibility: Descriptors beyond 2 [stderr] are made available to the child process as inheritable handles, but since the Windows architecture does not associate file descriptor numbers with low-level handles, the child process does not [yet] have a means of accessing those handles. Stdin, stdout and stderr work as expected.

Note:

If you only need a uni-directional [one-way] process pipe, use popen[] instead, as it is much easier to use.

See Also

  • popen[] - Opens process file pointer
  • exec[] - Execute an external program
  • system[] - Execute an external program and display the output
  • passthru[] - Execute an external program and display raw output
  • stream_select[] - Runs the equivalent of the select[] system call on the given arrays of streams with a timeout specified by seconds and microseconds
  • The backtick operator

devel at romanr dot info

10 years ago

The call works as should. No bugs.
But. In most cases you won't able to work with pipes in blocking mode.
When your output pipe [process' input one, $pipes[0]] is blocking, there is a case, when you and the process are blocked on output.
When your input pipe [process' output one, $pipes[1]] is blocking, there is a case, when you and the process both are blocked on own input.
So you should switch pipes into NONBLOCKING mode [stream_set_blocking].
Then, there is a case, when you're not able to read anything [fread[$pipes[1],...] == ""] either write [fwrite[$pipes[0],...] == 0]. In this case, you better check the process is alive [proc_get_status] and if it still is - wait for some time [stream_select]. The situation is truly asynchronous, the process may be busy working, processing your data.
Using shell effectively makes not possible to know whether the command is exists - proc_open always returns valid resource. You may even write some data into it [into shell, actually]. But eventually it will terminate, so check the process status regularly.
I would advice not using mkfifo-pipes, because filesystem fifo-pipe [mkfifo] blocks open/fopen call [!!!] until somebody opens other side [unix-related behavior]. In case the pipe is opened not by shell and the command is crashed or is not exists you will be blocked forever.

simeonl at dbc dot co dot nz

13 years ago

Note that when you call an external script and retrieve large amounts of data from STDOUT and STDERR, you may need to retrieve from both alternately in non-blocking mode [with appropriate pauses if no data is retrieved], so that your PHP script doesn't lock up. This can happen if you waiting on activity on one pipe while the external script is waiting for you to empty the other, e.g:

php at keith tyler dot com

12 years ago

Interestingly enough, it seems you actually have to store the return value in order for your streams to exist. You can't throw it away.

In other words, this works:



prints:
foo

but this doesn't work:



outputs:
Warning: stream_get_contents[]: is not a valid stream resource in Command line code on line 1

The only difference is that in the second case we don't save the output of proc_open to a variable.

aaronw at catalyst dot net dot nz

7 years ago

If you have a CLI script that prompts you for a password via STDIN, and you need to run it from PHP, proc_open[] can get you there. It's better than doing "echo $password | command.sh", because then your password will be visible in the process list to any user who runs "ps". Alternately you could print the password to a file and use cat: "cat passwordfile.txt | command.sh", but then you've got to manage that file in a secure manner.

If your command will always prompt you for responses in a specific order, then proc_open[] is quite simple to use and you don't really have to worry about blocking & non-blocking streams. For instance, to run the "passwd" command:

mcuadros at gmail dot com

9 years ago

This is a example of how run a command using as output the TTY, just like crontab -e or git commit does.



test_gen.php

daniela at itconnect dot net dot au

19 years ago

Just a small note in case it isn't obvious, its possible to treat the filename as in fopen, thus you can pass through the standard input from php like
    $descs = array [
                0 => array ["file", "php://stdin", "r"],
                1 => array ["pipe", "w"],
                2 => array ["pipe", "w"]
        ];
        $proc = proc_open ["myprogram", $descs, $fp];

andrew dot budd at adsciengineering dot com

16 years ago

The pty option is actually disabled in the source for some reason via a #if 0 && condition.  I'm not sure why it's disabled.  I removed the 0 && and recompiled, after which the pty option works perfectly.  Just a note.

MagicalTux at FF.ST

18 years ago

Note that if you need to be "interactive" with the user *and* the opened application, you can use stream_select to see if something is waiting on the other side of the pipe.

Stream functions can be used on pipes like :
- pipes from popen, proc_open
- pipes from fopen['php://stdin'] [or stdout]
- sockets [unix or tcp/udp]
- many other things probably but the most important is here

More informations about streams [you'll find many useful functions there] :
//www.php.net/manual/en/ref.stream.php

Anonymous

14 years ago

I needed to emulate a tty for a process [it wouldnt write to stdout or read from stdin], so I found this:



pipes are bidirectional then

weirdall at hotmail dot com

5 years ago

This script will tail a file using tail -F to follow scripts that are rotated.

joachimb at gmail dot com

14 years ago

I'm confused by the direction of the pipes. Most of the examples in this documentation opens pipe #2 as "r", because they want to read from stderr. That sounds logical to me, and that's what I tried to do. That didn't work, though. When I changed it to w, as in


everything works fine.

Kevin Barr

16 years ago

I found that with disabling stream blocking I was sometimes attempting to read a return line before the external application had responded. So, instead, I left blocking alone and used this simple function to add a timeout to the fgets function:

// fgetsPending[ $in,$tv_sec ] - Get a pending line of data from stream $in, waiting a maximum of $tv_sec seconds
function fgetsPending[&$in,$tv_sec=10] {
    if [ stream_select[$read = array[$in],$write=NULL,$except=NULL,$tv_sec] ] return fgets[$in];
    else return FALSE;   
}

stevebaldwin21 at googlemail dot com

7 years ago

For those who are finding that using the $cwd and $env options cause proc_open to fail [windows]. You will need to pass all other server environment variables;

$descriptorSpec = array[
       0 => array["pipe", "r"],
       1 => array["pipe", "w"],    
    ];

proc_open[
        "C:\\Windows\\System32\\PING.exe localhost,
        $descriptorSpec ,
        $pipes,
        "C:\\Windows\\System32",
        array[$_SERVER]
];

exel at example dot com

9 years ago

pipe communications may break brains off. i want to share some stuff to avoid such result.
for proper control of the communications through the "in" and "out" pipes of the opened sub-process, remember to set both of them into non-blocking mode and especially notice that fwrite may return [int]0 but it's not an error, just process might not except input at that moment.

so, let us consider an example of decoding gz-encoded file by using funzip as sub-process: [this is not the final version, just to show important things]

radone at gmail dot com

14 years ago

To complete the examples below that use proc_open to encrypt a string using GPG, here is a decrypt function:

Matou Havlena - matous at havlena dot net

12 years ago

There is some smart object Processes Manager which i have created for my application. It can control the maximum of simultaneously running processes.

Proccesmanager class:

Bài mới nhất

Chủ Đề