Answer the question
In order to leave comments, you need to log in
What is the correct way to run an external script in the background?
There is PHP 7.2 (CGI), for example, we add 2 simple scripts:
sleep.php:
<?php
sleep(30);
<?php
exec("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 &");
cd /var/www/user/data/www/example.com/ && /opt/php72/bin/php -q /var/www/user/data/www/example.com/background.php
Note:
If you are going to use this function in a program running as a daemon, make sure that the standard output of the function is directed to a file or other stream, otherwise PHP will hang until the end of the program.
Answer the question
In order to leave comments, you need to log in
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);
Everything works as it should, you just expect something strange.
The call to exec blocks until execution is complete.
The chain of calls via && is executed sequentially.
To spawn individual child processes, you can use pcntl_fork and so on.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question