PHP Exec Command (Or Similar) to Not Wait For Result

shell_exec without waiting for the output

This has been answered before at Asynchronous shell exec in PHP

Briefly:

shell_exec( $your_command . "> /dev/null 2>/dev/null &" );

Is there a way to use shell_exec without waiting for the command to complete?

How about adding.

"> /dev/null 2>/dev/null &"

shell_exec('php measurePerformance.php 47 844 email@yahoo.com > /dev/null 2>/dev/null &');

Note this also gets rid of the stdio and stderr.

Executing an exec() or system() in PHP and do not wait for output

Depends on the OS you are using.

For linux:

pclose(popen("php somefile.php &","r"));

notice the amperstand at the end (very important).

For windows:

pclose(popen("start php.exe somefile.php","r"));

here the start keyword is important.

Hope this helps.

shell_exec without waiting for the output

This has been answered before at Asynchronous shell exec in PHP

Briefly:

shell_exec( $your_command . "> /dev/null 2>/dev/null &" );

exec() waiting for a response in PHP

Depends on what platform you are using, and the command you are running.

For example, on Unix/Linux you can append > /dev/null & to the end of the command to tell the shell to release the process you have started and exec will return immediately. This doesn't work on Windows, but there is an alternative approach using the COM object (See edit below).

Many commands have a command line argument that can be passed so they release their association with the terminal and return immediately. Also, some commands will appear to hang because they have asked a question and are waiting for user input to tell them to continue (e.g. when running gzip and the target file already exists). In these cases, there is usually a command line argument that can be passed to tell the program how to handle this and not ask the question (in the gzip example you would pass -f).

EDIT

Here is the code to do what you want on Windows, as long as COM is available:

$commandToExec = 'somecommand.exe';
$wshShell = new COM("WScript.Shell");
$wshShell->Run($commandToExec, 0, FALSE);

Note that it is the third, FALSE parameter that tells WshShell to launch the program then return immediately (the second 0 parameter is defined as 'window style' and is probably meaningless here - you could pass any integer value). The WshShell object is documented here. This definitely works, I have used it before...

I have also edited above to reflect the fact that piping to /dev/null is also required in order to get & to work with exec() on *nix.

Also just added a bit more info about WshShell.

running php exec command in background

I will assume your running this on a *nix platform. To get php to run something in the background and not wait for the process to finish I would recommend 2 things: First use nohup and also redirect the output of the command to /dev/null (trash).

Example:

<?php
exec('nohup sar -u 1 > /dev/null 2>/dev/null &');

nohup means we do not send the "hang up" signal (which kills the process) when the terminal running the command closes.

> /dev/null 2>/dev/null & redirects the "normal" and "error" outputs to the blackhole /dev/null location. This allows PHP to not have to wait for the outputs of the command being called.

On another note, if you are using PHP just to call a shell command, you may want to consider other options like Ubuntu's Upstart with no PHP component--if you are using Ubuntu that is.

Do not wait for a respondse of PHP shell_exec

you should use exec() and redirect the output to a file or null it will then run in the back ground

If a program is started with this 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.

PHP execute command and log output without waiting

On Linux you can do:

exec('command* > /dev/null 2>/dev/null &');

On Windows you can do:

pclose(popen('start /B cmd /C "command* >NUL 2>NUL"', 'r'));

Both examples disable output and errors, those go to /dev/null (linux) or NUL (windows) which means they are stored "nowhere".

You can replace these with valid paths on your system.

On Linux, a & at the end places it into background. On windows this is more complicated and needs start to invoke the process and cmd to allow redirection of the streams.



Related Topics



Leave a reply



Submit