From the documentation:

In order to execute a command and have it not hang your PHP script while
it runs, the program you run must not output back to PHP. To do this,
redirect both stdout and stderr to /dev/null, then background it.

> /dev/null 2>&1 &

In order to execute a command and have
it spawned off as another process that
is not dependent on the Apache thread
to keep running (will not die if
somebody cancels the page) run this:

exec('bash -c "exec nohup setsid your_command > /dev/null 2>&1 &"');

Answer from Cristian on Stack Overflow
🌐
PHP
php.net › manual › en › function.shell-exec.php
PHP: shell_exec - Manual
If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg: Won't always work: echo shell_exec("gunzip -c -t $path_to_backup_file"); Should work: echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1"); In the above example, a line break at the beginning of the gunzip output seemed to prevent shell_exec printing anything else. Hope this saves someone else an hour or two. ... To run a command in background, the output must be redirected to /dev/null. This is written in exec() manual page. There are cases where you need the output to be logged somewhere else though. Redirecting the output to a file like this didn't work for me: <?php # this doesn't work!
Discussions

How to Wait for completion of shell commands execution in PHP? - Stack Overflow
i am executing long running shell commands using PHP. in a single php file i am calling a series of shell_exec function to execute long runnable commands. What's the problem is ,that i am getting a More on stackoverflow.com
🌐 stackoverflow.com
Executing Python Script from PHP and wait for it.
https://stackoverflow.com/questions/19735250/running-a-python-script-from-php Only found that but I guess you started by it More on reddit.com
🌐 r/PHPhelp
5
1
August 22, 2019
queue - PHP shell_exec wait for script to finish? - Stack Overflow
I have a PHP script that queries a database for a list of jobs to be done and fires off other PHP scripts based on what it finds in the database (basically a process queue). Some of the scripts t... More on stackoverflow.com
🌐 stackoverflow.com
September 3, 2011
shell script - Bash to Call PHP, Wait for PHP Process to finish? - Unix & Linux Stack Exchange
The way you have written it, it should wait until the script exits. Presumably, your php script is configured to be run in the background. Can you show us the contents of the PHP script? By the way, to check if it is running, use pgrep -f Cron-Run.php instead. ... Its a collecton of 34 Files all in all. the file i am attempting to execute ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
March 2, 2016
🌐
Linux Tip
linuxscrew.com › home › programming › php › php shell_exec function: how to use it [with examples]
PHP shell_exec Function: How to Use It [With Examples]
February 23, 2021 - $fileList = shell_exec('ls -la 2>&1'); echo "<pre>$fileList</pre>"; 2>&1 tells the shell to redirect stderr output to the current stdout – which resolves the issue. To run a command in the background, redirect the output to /dev/null – this sends the output to nowhere (effectively sending it to the abyss), rather than PHP waiting for the response, it will execute the code and resume running PHP immediately – the result from shell_exec will not be waited on, and will not be available to PHP.
🌐
Stack Overflow
stackoverflow.com › questions › 36576674 › how-to-wait-for-completion-of-shell-commands-execution-in-php
How to Wait for completion of shell commands execution in PHP? - Stack Overflow
PHP shell_exec update output as script is running · Share · Improve this answer · Follow · edited May 23, 2017 at 12:07 · CommunityBot · 111 silver badge · answered Apr 12, 2016 at 14:48 · Hyra · 82811 gold badge66 silver badges2222 bronze badges · Sign up to request clarification or add additional context in comments. Add a comment · 0 · shell_exec waits for command, so, problem is, as u think, you're calling it into background, maybe a nohup?.
🌐
Reddit
reddit.com › r/phphelp › executing python script from php and wait for it.
r/PHPhelp on Reddit: Executing Python Script from PHP and wait for it.
August 22, 2019 -

I was working on something that requires me to run a Python script with some arguments from PHP itself, and complete some task and print some output. The thing is, I tried shell_exec() and few more things mentioned in SO to run python script. But, the issue is that PHP doesn't seem to wait for my python script to complete. I need something that'll wait till the Python is done with the job. Can someone suggest me something?

EDIT : Typo in "shell_exec()" name.

UPDATE : After I re-looked at my script this morning.... seems like python can't open file. So, the PHP might not have permissions for that script. So, went ahead and gave it the perms. I added "www-data" group to that python file and changed python file's perms to 777. It still didn't work. But, the main issue right now is that Python script can't seem to be executed via apache2->Php.

Thanks everyone for all the help and hints. Appreciate it!

UPDATE 2 : I fixed the permission issue with "www-data" and it's working. THANKS EVERYONE!

🌐
Our Code World
ourcodeworld.com › articles › read › 207 › how-to-execute-a-shell-command-using-php-without-await-for-the-result-asynchronous-in-linux-and-windows-environments
How to execute a shell command using PHP without await for the result (asynchronous) in Linux and windows environments | Our Code World
July 17, 2016 - If you use the command to execute ... as the default time limit is of 30 seconds. The system and shell_exec methods always expects for the output, there's no way to prevent it, however we use the trick to redirect a null output ...
Find elsewhere
🌐
Stack Exchange
unix.stackexchange.com › questions › 267105 › bash-to-call-php-wait-for-php-process-to-finish
shell script - Bash to Call PHP, Wait for PHP Process to finish? - Unix & Linux Stack Exchange
March 2, 2016 - I was able to pause / wait for the process to finish by using the wait function. ... #!/bin/bash timestamp() { date +"%T" } pushd 'httpdocs/DIR1/DIR2'; file="Lock3.key" if [ -f "$file" ] then echo "$file found, Session is already in progress" $(date) else echo "$file was not found, Its safe to start the script" $(date) echo "Running Ebay Stock Only Script" $(date) #Its possible to double check if the script is running by checking memory. #ps aux | grep "php Cron-Run.php" (Not safe) php Cron-Run.php > Output.log wait echo "Finished Running Script, Lock file is deleted by PHP, Now exiting" $(date) fi exit
🌐
SitePoint
sitepoint.com › php
Long running PHP process waiting on shell exec - PHP - SitePoint Forums | Web Development & Design Community
July 29, 2010 - I have a PHP script that loops through a folder of images, for each image it adds some info to the database and resizes the image by making a shell call to ImageMagick. The problem is that this script takes a long time to execute since it waits for ImageMagick to resize each image before going ...
🌐
Subinsb
subinsb.com › how-to-execute-command-without-waiting-for-it-to-finish-in-php
Execute Command Without Waiting For It To Finish - Subin's Blog
function <span style="color: red;">bgExec</span>($cmd) { if(substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); }else { exec($cmd .
🌐
Dubbelboer
blog.dubbelboer.com › 2012 › 08 › 24 › execute-with-timeout.html
PHP shell_exec with a timeout - Erik's Code
<? /** * Execute a command and return it's output. Either wait until the command exits or the timeout has expired. * * @param string $cmd Command to execute. * @param number $timeout Timeout in seconds. * @return string Output of the command. * @throws \Exception */ function exec_timeout($cmd, $timeout) { // File descriptors passed to the process.
🌐
Piotr Horzycki
peterdev.pl › execute-a-shell-command-in-php
Executing shell commands from a PHP script | Piotr Horzycki - Java and PHP developer’s blog
April 2, 2021 - Such mechanism is commonly used in terminals, for example to paginate long output: ... In the example above, the output of the ls command was sent to the less command. If the ls failed, the chain would break and the second command would not be called. By default, we have to wait until each process terminates. This means our PHP script will also be paused when executing an external command.
🌐
Reddit
reddit.com › r/phphelp › php shell_exec not working with pipes
r/PHPhelp on Reddit: php shell_exec not working with pipes
May 28, 2022 -

Hi all I'm trying to SvtAv1EncApp tools via web interface, I try to us both exec() shell_exec() with no success , BTW I print the command and run it into the shell directly and it's works perfectly fine.

$cmd2="/usr/local/bin/ffmpeg -loglevel -8 -i "$mp4" -s 960x540 -strict -1 -f yuv4mpegpipe - | /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log | /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "$mp4" -map 0:v -map 1:a:0 -c:v copy $a '".$mpa."_.mkv' & ";

shell_exec( $cmd2 ) ;

Here is the text of echo $cmd2 output

/usr/local/bin/ffmpeg -loglevel -8 -i "FHD.mp4" -s 960x540 -strict -1 -f yuv4mpegpipe - | /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log | /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "FHD.mp4" -map 0:v -map 1:a:0 -c:v copy -strict -2 -c:a libopus -b:a 64k 'FHD_.mkv' &

=Update=

I kinda solve it , I don't know why but exec() successfully executed the script , I created new bash script from command line thanks to @xisonc suggesting nano /usr/local/sbin/av1c with this value

!/bin/sh

touch /var/www/vl/ffmpeg.log /usr/local/bin/ffmpeg -loglevel -8 -i "$1" -s 960x540 -strict -1 -f yuv4mpegpipe -
| /usr/local/bin/SvtAv1EncApp --no-progress -i stdin --rc 0 -q 38 --preset 8 -b stdout 2>/var/www/vl/ffmpeg.log
| /usr/local/bin/ffmpeg -loglevel -8 -y -i - -i "$1" -map 0:v -map 1:a:0 -c:v copy -strict -2 -c:a libopus -b:a 64k "$2"

On the php script i had this

$cmd2="/usr/local/sbin/av1c '$mp4' '$mpa"."_.mkv'   2>/dev/null >/dev/null & " ; 
exec( $cmd2 , $pid, $r )    ;   
var_dump(  $r )                ;

var_dump returns 0 , and since the bash script are silent I don't need to see the output , now I want to change it to wok on the background it's running on background now after adding 2>/dev/null >/dev/null & at the end of the command .

🌐
Medium
lakin-mohapatra.medium.com › achieve-parallel-processing-in-php-using-shell-exec-af99857d023c
Achieve parallel processing in php using shell_exec() | by Lakin Mohapatra | Medium
December 25, 2021 - The Client App asks the MQ Engine for a data (message) to be processed them in order (FIFO or based on priority) “you can also request data from specific queue". ... Now we will specifically discuss achieving parallel processing using shell_exec() command. // Set data which needs to be passed to separate process running php file. $arguments = [ 'data' => [], 'msg' => 'Test Message'];// Set Redirect output path to run php file in background rather than waiting for its output.