PHP hangs and exec () bash script

I have a few lines of code that look like this:

exec($this->path.' start > /dev/null 2>&1 &');
return ['status' => 'Command executed'];

$this->pathis the shell of the script, the beginning is the argument for the shell of the script, and I believe that the rest of the line should discard any response so that the PHP script can continue. It does not work as it should, php successfully starts the shell script (which starts the game server), however php just freezes until I close the server using the shell. When I close the server using the shell, it completes the execution, and I get the response "command completed". I also disabled SELinux forcing to make sure it doesn't interfere.

Running Linux - Fedora 21 and the embedded PHP development server.

+4
source share
3 answers

I believe the rest of the line should discard any response, so php script may keep working

If you do not understand this, here is an explanation. If you have:

exec($this->path.' start > /dev/null 2>&1 &');

The part > /dev/nullmeans redirecting stdout (i.e. regular output generated by the command) to / dev / null (which is a null device). Therefore, any output created by the team itself will be suppressed.

Part 2>&1means stderror redirection (i.e. any errors that occur when executing the command) to stdout. However, since stdout is redirected to / dev / null, any errors will also be redirected there. Therefore, with these two, it suppresses any messages that will ever be created by the team.

, & () . Bash man:

&, . , 0 (true).

, , , , . - , . PHP - , , PHP exec. , , . - . PHP, set -m ( ). : set -m set +m. PHP:

exec('set -m && ' . $this->path.' start > /dev/null 2>&1 &');

, , - PHP script, jobs . , PHP . - :

[1]+  Stopped                 your_command.sh

, stopped. , stopped, .

, , - , checkjobs. , :

shopt -p | grep checkjobs

shopt -u checkjobs, . shopt -s checkjobs, , , , , , , . , , PHP . shopt -u checkjobs && PHP.

exec('shopt -u checkjobs && ' . $this->path.' start > /dev/null 2>&1 &');
+3

, :

pclose(popen($this->path.' start > /dev/null 2>&1 &', 'r'));

, , ,

, :)

0

A () - script .

exec('screen -dmS -X ' . $this->path . ' start');
0

Source: https://habr.com/ru/post/1569747/


All Articles