php exec command (or similar) to not wait for result

php exec stderr
running shell command in php with real time output
php exec without waiting windows
php cmd shell
php shell_exec not working
php system command
php exec() vs shell_exec
php exec asynchronous

I have a command I want to run, but I do not want PHP to sit and wait for the result.

<?php
echo "Starting Script";
exec('run_baby_run');
echo "Thanks, Script is running in background";
?>

Is it possible to have PHP not wait for the result.. i.e. just kick it off and move along to the next command.

I cant find anything, and not sure its even possible. The best I could find was someone making a CRON job to start in a minute.

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 &"');

exec - Manual, If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included​  Is it possible to have PHP not wait for the result.. i.e. just kick it off and move along to the next command. I cant find anything, and not sure its even possible. The best I could find was someone making a CRON job to start in a minute.

You can run the command in the background by adding a & at the end of it as:

exec('run_baby_run &');

But doing this alone will hang your script because:

If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

So you can redirect the stdout of the command to a file, if you want to see it later or to /dev/null if you want to discard it as:

exec('run_baby_run > /dev/null &');

How to execute a shell command using PHP without await for the , No one wants to see a progressbar for hour expecting for an answer <?php /** * Executes a console command (like execute the command via putty) in * a linux environment or windows from php without await for the result. On Windows-Apache-PHP servers there is a problem with using the exec command more than once at the same time. If a script (with the exec command) is loaded more than once by the same user at the same time the server will freeze. In my case the PHP script using the exec command was used as the source of an image tag.

This uses wget to notify a URL of something without waiting.

$command = 'wget -qO- http://test.com/data=data';
exec('nohup ' . $command . ' >> /dev/null 2>&1 & echo $!', $pid);

This uses ls to update a log without waiting.

$command = 'ls -la > content.log';
exec('nohup ' . $command . ' >> /dev/null 2>&1 & echo $!', $pid);

php exec command (or similar) to not wait for result, You can run the command in the background by adding a & at the end of it as: exec('run_baby_run &');. But doing this alone will hang your script  php exec command (or similar) to not wait for result (6 answers) Closed 5 years ago . 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).

"exec nohup setsid your_command"

the nohup allows your_command to continue even though the process that launched may terminate first. If it does, the the SIGNUP signal will be sent to your_command causing it to terminate (unless it catches that signal and ignores it).

php exec command (or similar) to not wait for result, I have a command I want to run, but I do not want PHP to sit and wait for the result​. <?php echo "Starting Script"; exec('run_baby_run'); echo "Thanks, Script is  It really all comes down to how you want to handle output that the command might return and whether you want your PHP script to wait for the callee program to finish or not. exec executes a command and passes output to the caller (or returns it in an optional variable). passthru is similar to the exec() function

There are two possible ways to implement it. The easiest way is direct result to dev/null

exec("run_baby_run > /dev/null 2>&1 &");

But in case you have any other operations to be performed you may consider ignore_user_abort In this case the script will be running even after you close connection.

Execute Command Without Waiting For It To Finish, So, by the function you can call the command like this : bgExec("php -q server.​php");. Use it as you wish. Previous Post. exec() Function vs shell_exec() Function. PHP also provides the shell_exec() function which is very similar to the exec() function. The main difference is shell_exec() function accept single parameter which is command and returns the output as string.

Performing asynchronous php requests from within a script + don't , However, I'd like this script to execute as fast as possible and some of these So I just would like to send data via HTTP, and don't wait for any response. I had a perl program which ran fine from command line but not using shell_exec(), exec() or system() - nothing was being returned. I was using the full path and permissions were set correctly. It turned out the perl program was using more memory than my PHP.INI file was set to allow.

How to run a php script in background, When we need to run some script without waiting the fronted user till You can type other command while background command is running. system() is just like the C version of the function in that it executes the given command and outputs the result. The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.

PHP, It also returns NULL if no command run properly. Syntax: string exec( $command, $output, $return_var ). Parameters: This function accepts three parameters as  I want to develop a console for php and I wanted to know how to retreive and output the result of a unix command. At first I outputed the command to a file and read the file with php but some commands don't allow the "> filename" option.

Comments
  • If you have to do this frequently, you might also be interested in a JobQueue, like Gearman.
  • Thank you so much! There are lots of "solutions" which don't actually work (The main browser thread is still waiting). Many solutions only tell about the "&" at the end which is not sufficient. That said, for me a simpler solution worked to: exec('nohup your_command > /dev/null 2>&1 &') was enough.
  • Why don't you need to also port stdin to /dev/null? Will a process' input being linked to a shell be enough to stop nohup from "disowning" the child process? (They do change stdin in this answer: superuser.com/questions/178587/…)
  • also, why do you call both nohup and setsid? I can't find much information about which one is better or how they are different.. (stackoverflow.com/questions/10247721/…)
  • @Christin: Your solution is working for my question: stackoverflow.com/questions/25700904/… can you put an answer to that question. Then i can close that question with a green answer.
  • @Christin : Can you please take a look at my issue it seems same as my issue : stackoverflow.com/questions/35331958/…
  • Doesn't redirect stderr, and gets killed by a crashing webserver.
  • I think you are missing an & symbol between 2>&1 and echo, am I right?
  • Does nohup also keep the child running if the parent is killed?
  • @santiagoarizti yes, I fixed it as the code was not working otherwise
  • According to this answer, a child has to sign up for SIGHUP to be notified of its parent's death. A terminal tends to send the HUP signal on exit, though.