I have PHP script, which is requested by user. In this script I want to run C++ script(exe), but not wait until this script finishes(because this script does some post-process with data, which PHP doesn't care about). Is there anyway to do this on Apache?
You want the php exec function. Bear in mind the comment on the page about apache:
When calling exec() from within an
apache php script, make sure to take
care of stdout, stderr and stdin (as
in the example below).
Related
I have a shell script which is running in background and i want to know the shell script execution completion and send it to the PHP which is running in browser currently.PHP and shell script has no link now.How to get the shell script completion and signal it to the PHP
I understand this is more a conceptual question. Two simplest methods that come into mind are:
1) Use a "flag file" that you create from the shell script. When the file exists, it means the script is done. When there's no file, the script still runs. Or other way around. That's common method for shell scripts anyway.
For more background info on how to create this from your shell script, see http://www.cyberciti.biz/tips/shell-scripting-bash-how-to-create-empty-temporary-file-quickly.html
From your PHP code, you test for the (non)existence of that file. PHP has a lot of functions for that, eq. look into file_exists ( string $filename ), see http://php.net/manual/en/function.file-exists.php
2) From the shell script, use a URL parameter when you call the php script and from your PHP code, you read the parameter and work with that. PHP has a lot of functions for that (eg. look into $_SERVER['QUERY_STRING']). Some more on using the $_SERVER vars, see http://php.net/manual/en/reserved.variables.server.php
Hope this helps...
Here you got everything you need, examples, explanations and more.
http://php.net/manual/en/function.shell-exec.php
Okay, this is going to be a very weird request/question.
There is a very long running PHP script that needs to be launched by the user (admin) who is not very technically adept. When running the script through apache, it throws a timeout (502 or 504 Bad Gateway).
Let's just assume that apache can't be configured to fix the timeout issues.
I want to create a button in the admin panel that sends an AJAX call to a PHP script on the server, that PHP script will act as a proxy of sorts to launch a shell command. The shell command will then execute the long running PHP script with certain arguments... but I don't want it to wait for the long running script to finish. The proxy PHP script can exit and return true/false based on if the shell command actually started (this part is optional).
Essentially, have PHP launch a shell command which launches a PHP script.
How can I pull something like this off?
Have you tried shell_exec. It worked for me...
http://php.net/manual/en/function.shell-exec.php
is it possible to launch a php script in background on the webserver with js and let it run even if you change page or not visit the site at all and then get the current status if you call the php script in a second moment?
This php script will process data for hours and sleep for X seconds/minutes for each loops. If what I asked before is possible how can I even get "echos" from it if php will only generated an output only when the script ends?
Maybe this is not a job for PHP?
thank you
EDIT: on a windows machine with apache
It certainly is possible - I have several scripts that run 24/7 written in PHP. Check out Creating Daemons in PHP. It has good info on how to 'daemonize' a php script so that it will run like a service, and it also covers signal handling.
To get debugging output you would redirect to a log file. Do a search on "unix redirect output" as there is a lot of info available.
In Windows it's not much different from UNIX.
First of all, you need to create a PHP script with a run loop. For example, take a look at this: http://code.google.com/p/php-apns/ . This is a PHP "daemon": the main script, PushMonitor.php, runs forever, because it has an infinite loop. It polls a queue at regular intervals, then execute the actions and then wait. Really simple, actually!
The problem, in your case, is that you want to launch the "daemon" from a PHP script.
You may want to look at this: http://robert.accettura.com/blog/2006/09/14/asynchronous-processing-with-php/ (first example code) . You will execute something like launchBackgroundProcess('php myscript.php') .
Note that on the code there's the "start /b" command (and the "&" at the end of the command for UNIX). That is important, because otherwise your process would be killed when the PHP script of the web page is terminated (children process die after parent dies!).
Also, remember that the "php" executable (cli) must be in your path (so you can execute "php" from the command line).
Since the PHP script of the page launching the background process is going to terminate, you can't directly catch the "echoes" in a simple way. My suggestion is to write all output to a file (or a database etc), and then read the contents from that source when necessary.
So, instead of "echo", you will use file_put_contents() etc.
As an extension to question "php execute a background process":
Suppose I wanted to keep a process running during a PHP session, like an interactive bash shell. How can I establish redirection of stdout/stdin such that PHP can read/write to the process?
UPDATE: The key is to be able to kick off the process and keep it running in the background so that later requests can access its stdout/stdin streams.
I would use PHP Expect. The documentation has some very good usage examples.
If you're using Linux, you can access the proc file system at /proc. Though distributions may differ somewhat, in Ubuntu Server I can find my stdio at /proc/<pid>/fd/[012]. 0 is stdin, 1 is stdout, and 2 is stderr. This will probably not work if you are redirecting these from or to /dev/null, as some methods of spinning off long running background processes have you do. If you have access to the pid of the background process (a la http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/), you should be able to access the stdin / stdout of the process in Linux.
If you're using PHP to relay user-typed commands to a shell, you can use mulitple calls to shell_exec
shell_exec will return the complete output which you can then echo back to the user.
i start a linux console app from my php5 script, it starts ok but then termintates. I've tried using system(), shell_exec and tried starting as background process but to no avail it starts and then quits.
What i am trying to achieve is from a remote browser start a console app using a php5 script and then it should remain running (just as it would if i started it from a bash shell) , i then want to send commands (from a bash shell it would be keyboard strokes) to the console app from another set of php5 scripts. Hope its clear what i am trying to do.
If anyone could give some info on the best way about doing this, as i think i may have something fundamentally wrong.
I have a Debian Lenny box running apache.The console app is just a simple program that prints to stdout and reads from stdin.
How do you expect to send input to this app? Where is it listening for input?
It simply may only support interactive use, and exit as a result of that. Or, even simpler, it may terminate because it sees that is has no input (nothing piped in or nothing from some file) and since it's not connected to an interactive shell, it has nothing to do. There's no point in waiting for input from a user that doesn't have a way to interact w/ the application.
On every request, PHP starts up, compiles your script and executes it. After execution, the script exists. When the script exits, all of the resources it was using, including file handles, database handles, and pipes to other programs are terminated.
You're going to need to find another way to keep your program open and have PHP communicate with it. Otherwise, every request to your script is going to open a new copy of the program, and then both will exit when the PHP script is complete.
Unfortunately without knowing what the program is, it will be hard to offer suggestions on how to go about doing this.