How to run an external script in the background?

0 like 0 dislike
7 views
Is PHP 7.2 (CGI), for example add 2 simple script:

sleep.php:
<?phpsleep(30);</code>


background.php:
<?phpexec("su -s /bin/sh - user -c cd /var/www/user/data/www/example.com/ && /opt/php72/bin/php -q /var/www/user/data/www/example.com/sleep.php > /dev/null 2>&1 &");


Console:
cd /var/www/user/data/www/example.com/ && /opt/php72/bin/php-q /var/www/user/data/www/example.com/background.php


In the file background.php we run the command in the background, but the script hangs for 30 seconds, why is this happening?

In the documentation it says:

Note:
If you are going to use this function in a program running as a daemon, make sure that the standard function output directed to a file or another stream, otherwise PHP will hang until the end of program execution.


The problem with the environment? How it is adjusted? What am I doing wrong?
by | 7 views

2 Answers

0 like 0 dislike
background.php:
$command = "(su -s /bin/sh - user-c cd /var/www/user/data/www/example.com/ && /opt/php72/bin/php-q /var/www/user/data/www/example.com/sleep.php) >/dev/null 2>&1 &"; exec($command);


\rhttps://florian.ec/articles/running-background-pro...
\rhttps://stackoverflow.com/questions/14541741/how-d...
\rhttps://unix.stackexchange.com/questions/3886/diff...
by
0 like 0 dislike
Everything works as you must be something weird just waiting.

The exec is blocked until execution.
Calling chain using && is executed sequentially.

For the generation of individual child processes, you can use pcntl_fork, etc.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
6 answers
asked Mar 22, 2019 by Mirgorod
0 like 0 dislike
3 answers
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
asked Mar 24, 2019 by yitzhakv
110,608 questions
257,186 answers
0 comments
32,810 users