exec() waiting for a response in PHP [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php exec command (or similar) to not wait for result
I have a page that runs a series of exec() commands which forces my PHP script to halt alteration until it receives a response. How can I tell exec() to not wait for a response and just run the command?
I'm using a complex command that has a backend system I can query to check the status, so I'm not concerned with a response.

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.

In the past, I've had good luck with constructs like the following (windows, but I'm sure there's an equivalent command in *nix
pclose(popen('START /B some_command','r'));

What about running command in background ?
exec('./run &');

Related

What is the best practice to run PHP script from PHP script?

I'd use the following code:
$SERVER_PATH = dirname(__FILE__);
shell_exec($PHP_LOCATION.' '.$SERVER_PATH."/script.php?k1=v1&k2=v2 > /dev/null 2>/dev/null &");
Where:
$PHP_LOCATION should contain the path to PHP,
$SERVER_PATH - is current working directory (fortunately the script to run is in the same directory),
> /dev/null 2>/dev/null & added to make this call asynchronous (taken from Asynchronous shell exec in PHP question)
This code has two problems:
As far as I remember ?k1=v1&k2=v2 will work for web-call only, so in this particular case parameters will not be passed to the script.
I don't really know how to init the $PHP_LOCATION variable to be flexible and to work on the most hosts.
I conducted some research regarding both problems:
To solve 1 suggested to use -- 'parameters_string' but it is also recommended to modify the script to parse parameters string which looks a bit clumsy. Is there a better solution?
To solve 2 I found a solution to use PHP_BINARY but this is a PHP 5.4+ case (I'm using 5.3). But the original question was about to run PHP of the same version as the original script version. So for me (as I use PHP 5.3 only) is there probably a solution?
EDIT 0
Let me do some explanation why I stuck to this weird (for PHP) approach:
Those PHP scripts should be separate from each other:
one of those will analyze the data and
the second will generate PNG graphs as a final result.
Those scripts aren't intended to run simultaneously, this means that the second can run at it's own schedule it is only needed that the run should be upon its data will be ready (which done by the first script). So no data should be passed back from second script (child) to the first (parent).
EDIT 1
As seeing from most of the comments the main discussion goes to forking direction. However I'd like to make stress on the point 1 and 2 asked in the original questions. I have some reasons to solve the task in the way I pointed out and I tried to point all that reason. If some of my points looks weird, please post a comment - I will make it more clear or I will change the main question.
Thank you in advance!
How to get executable
Assuming you're using Linux, you can use:
function getBinaryRunner($binary)
{
return trim(shell_exec('which '.$binary));
}
For instance, same may be used for checking if needed stuff is installed:
function checkIfCommandExists($command)
{
$result = shell_exec('which '.$command);
return !empty($result);
}
Some points:
Yes, it will work only for Linux
You should be careful with user input if it is allowed to be passed to shell commands: escapeshellarg() and company
Indeed, normally PHP should not be used for stuff like this as if it is about asynchronous requests, better to either implement forking or run commands from external workers.
How to pass parameters
With doing shell_exec() via file system path you're accessing the file and, obviously, all "GET" parameters are becoming just part of file name, it is no longer "URI" as there is no web-server to process that. So you have two options:
Invoke call via accessing your web-server. So it will be like:
//Yes, you will use wget or, better, curl to make web-request from CLI
shell_exec('wget http://your.web-server.domain/script.php?foo=bar');
Downside here: if you'll access your web-server via public DNS, it will cause network gap and all processing overheads. Benefit - obviously, you will not have to expect anything else in your script and make no distinction between CLI and non-CLI calls
Use $_SERVER array in your script and pass parameters as it should be with CLI:
shell_exec('/usr/bin/php /path/to/script.php foo bar');
//inside your script.php you will see:
//$_SERVER['argv'][0] is "script.php"
//$_SERVER['argv'][1] is "foo"
//$_SERVER['argv'][2] is "bar"
Yes, it will require modification in the script, and, probably, some logic of how to map "regular" web-requests and CLI ones. I would suggest even to think of separating CLI-related stuff to different scripts bundle so not to mess that logic.
More about "asynchronous run"
When you do php script.php & you just run it in background mode. That, however, still keeps parent-child relation for your process. That means - if parent process dies, it's childs will also be removed. To be precise, SIGHUP will be triggered and to avoid this situation you should use nohup command. It will allow to emulate "detaching" of a process and therefore making it's run reliable and independent of circumstances happening to parent process.

php run another script in foreground

I have a php script that leads up to running another expect script by passing it arguments.
$output = shell_exec("expect login_script.tcl '$user' '$host' '$port' '$password'");
Using shell_exec doesn't work as the script gets run in the background or 'within' the php script. I need it to run in the foreground, allowing user interactivity. Is there an elegant way to do this. Already it is getting messy by having to use different scripting languages. I tried wrapping the two scripts with a shell script that called the php script, assigned output the result as a variable (which was a command) and then ran sh on that. However I have the same problem again where the scripts are run in the background and any user interactivity creates a halt/freeze. Its ok in this situation if php 'quits' out when calling shell exec. Ie. php stops and expect gets run as if you called it. (the same as if i just copied the command that is output and pasted it into the terminal).
Update
I am having much more luck with the following command in php:
shell_exec("gnome-terminal -e 'bash -c \"expect ~/commands/login_script.tcl; exec bash\"' &");
However, can this be improved in order to not close the shell immediately after the secondary script (login_script) is finished?
Further Update
From reading the answers I think I need to clarify things as it looks like people are assuming a 'more complicated' issue.
the two processes do not need to communicate with each other, I should probably not have put the $output = shell_exec in the example and just shell_exec on its own as I believe this has led to the confusion.
The php script needs to only initiate the expect script with some cli parameters, e.g. my-script 'param1' 'param2' and can be thought of as completely 'asynchronous'. This is much like the behaviour of launcher programs like 'launchy' or 'synapse' they can launch other programs but need not affect them. Nor do they wait for the secondary program to quit/finish.
I made the mistake of saying 'shell_exec' doesn't work for me. What I should have said was that 'I have so far not succeeded with using shell_exec', but shell_exec("gnome-terminal -e 'bash -c \"expect ~/commands/login_script.tcl; exec bash\"' &"); is 'working' but still trying to find the right quote combination to allow passing arguments to the expect script.
Task managing is an interesting but difficult job.
Because your user can move during a task (and leads it to an unexpected result, such as session freezes, or an incomplete work from the process), you need to execute it in background. If you need to interact between your user and your process, you'll need to create a way to communicate.
The easiest way (I think) is to use a file, shared between your user session and the task.
If you have a lot of users simultaneously and communicates a lot between user and processes, you can mount a partition in memory to optimize the read/write operations.
In your fstab, a line like :
tmpfs /memory tmpfs defaults,uid=www-data,gid=www-data,size=128M 0 0
Or, in a script, you could do :
#!/bin/sh
mkfs -t ext2 -q /dev/ram1 65536
[ ! -d /memory ] && mkdir -p /memory
mount /dev/ram1 /memory
chmod -R 777 /memory
You'll need to take care of a lot of things :
file access (to avoid concurrency between your webapp and your processes)
time (to avoid zombies or useless long-running scripts)
security (such operations must be carefully designed)
resources management (to avoid that 10000 processes runs simuntaneouly)
...
I think what you're looking for is the proc_open() command. It gives you access to the stdin/stdout streams of the background process. You can pass your own stdin/stdout streams to the new process in the $descriptorSpec parameter, which will let your background process talk to the user.
Your 'foreground' application will have to wait around until the background process has died. I haven't actuallly done this with PHP, but I'm guessing you'll have to watch the $pipes to see when they get closed -- then you'll know the background process is finished and you can delete the process resource and continue on with whatever the foreground process needs to do.
In the end, I managed to get it working by by adding a third quotation mark type: ` (I believe it is called a 'tack'?) which allowed me to pass arguments to the next script from the first script
The command I needed in my php script was:
$command = `gnome-terminal -e 'bash -c "expect ~/commands/login_script.tcl \"$user\" \"$host\" \"$port\" \"$password\"; exec bash"' &`;
shell_exec($command);
It took a while to get all the quotes right as swapping the type of quotes around can lead to it not working.
Here is a video demonstrating the end result
Use:
pcntl_exec("command", array("parameter1", "parameter2"));
For example, I have a script that starts the mysql command using the parameters in the current php project that looks like:
pcntl_exec("/usr/bin/mysql", array(
"--user=".$params['user'],
"--password=".$params['password'],
"--host=".$params['host'],
$params['dbname']
));
This doesn't rely on gnome terminal or anything, it replaces PHP with the program you call.
You do need to know the full path of the command, which is a pain because it can vary by platform, but you can use the env command command which is available at /usr/bin/env on most systems to find the command for you. The above example above becomes:
pcntl_exec("/usr/bin/env", array(
"mysql",
"--user=".$params['user'],
"--password=".$params['password'],
"--host=".$params['host'],
$params['dbname']
));

Dbus PHP Unable to Launch dbus-daemon Without $DISPLAY for X11

I'm using GREE Labs' Dbus PHP Extension in my attempts to make a PHP class that is able to create desktop notifications.
$dbus = $dbus = dbus_bus_get(DBUS_BUS_SESSION);
$message = new \DBusMessage(DBUS_MESSAGE_TYPE_SIGNAL);
$message->setDestination("org.freedesktop.DBus");
$message->setAutoStart(true);
$dbus->sendWithReplyAndBlock($message, 1);
When my code is run I get the following error:
Warning: dbus_bus_get() [function.dbus-bus-get]: failed to create dbus
connection object [Unable to autolaunch a dbus-daemon without a
$DISPLAY for X11] in [...COI/GTK/Notify.php on line 39
This is the first time I've used anything related to dbus, and am rather lost.
I'm aiming for an effect similar to what occurs when one executes the following in a terminal (on Ubuntu 11.10):
/usr/bin/notify-send -t 2000 'title' 'message'
I did initially use notify-send & exec, but switched when I found the GREE Dbus extension as I thought it may provide a cleaner interface. Also notify-send would only work properly if I changed my apache user to be the same as the user I'm currently logged in as - not an ideal solution.
Would anyone be able to either tell me what modifications are required to achieve my desired result, or alternatively tell me if what I want to do is in fact impossible?
Or, is there another way I should be doing this?
Dbus does not like being run while in a command line environment, without X. It's frustrating, but this is what I wrote in python to override that. It comes down to setting two environmental variables.
def run(self):
os.environ['DBUS_SESSION_BUS_ADDRESS'] = "unix:path=/run/dbus/system_bus_socket"
os.environ["DISPLAY"] = ":0"
try:
bus_name = dbus.service.BusName(INTERFACE,
bus = dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name,
'/com/your/path/here')
gobject.MainLoop().run()
except Exception, E:
f = file('/tmp/bus.log', 'a')
f.write(str(E))
f.close()
EDIT: I forgot another very useful way to run dbus on the command line
eval 'dbus-launch --auto-syntax' [command]
I use it on a raspberry pi to run my custom dbus deamons. dbus-launch --auto-syntax is a command that outputs environmental variables and files applicable to dbus in bash. The eval command will take that output and evaluate it so your command will run with those environmental variables.
A simple test would be to run something like this:
eval 'dbus-launch --auto-syntax' python /usr/bin/my-dbus-daemon.py
eval 'dbus-launch --auto-syntax' qdbus org.dbus.method /org/dbus/method/test
Use dbus-launch in the script that starts your web server in order to start an appropriate DBus daemon at the same time. See the dbus-launch(1) man page for details.

PHP run a non blocking system call

How can I run a non blocking system call in PHP?
The system call will call a streaming service run by a second PHP script.. So my page sits and waits on this call.
My two thoughts on a solution:
1: There exists a native method / parameter to execute a system call by non blocking
2: Run system() on a new C++ program that will then fork itself and run the actual php script, on a sep. thread
Is there a native method of executing system calls in a non blocking manner or do I need to hack around this...
I currently have shell_exec('nohup php /path/to/file.php &') but it still holds
From PHP manual:
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.
An example is provided in a comment on the same page (linux based):
If you want to start a php process that continues to run independently
from apache (with a different parent pid) use nohub. Example:
exec('nohup php process.php > process.out 2> process.err < /dev/null
&');

Run shell commands with PHP?

Occasionally my media server goes down and I'm wondering if it's possible to start it remotely using php to check the port and if it's not running invoke cron (or some other way) to run a shell command. Is this possible because this is not a strong area for me. Here's the process I use with PuTTy.
login to shell
cd to source/red5/dist
screen
./red5.sh
CTRL-A then D to detach
logout
Simplest thing is to write a shell script. And then login to remote console via PHP.
shell_exec: execute a shell command and returns the output as string.
exec: just executes an external program
A simple way to achieve what you want is to run this in screen:
while /bin/true ; do ./red5.sh ; done
If you can write a shell script that does what you need, then PHP's has exec(), system() and passthru() for you.
PHP actually has a special operator for executing shell commands, the backtick:
`cd source/red5/dist`
will go to the specified directory. (But I don't know much about shell, so I can't implement you the whole thing.)
If you need much control over the execution (I don't know whether you need here) use proc_open.
you can use corn job on php and put all command on .sh file and run like this
59 11 * * 1,2,3,4,5 root command file.sh?token
something like this ,it will be save
There is more than one good answer here, but you should opt for executing the init script for red5 instead of the .sh or .bat. There are pre-made init scripts here: http://code.google.com/p/bigbluebutton/downloads/detail?name=red5&can=2&q= and here: http://www.videowhisper.com/forum.php?ftid=48&t=init-file-red5-linux-installations-red5-linux-init.d-chkconfig

Categories