I am trying to run a program using PHP and keep sending output to it. I've tried using exec() but as the documentation page says, it hangs, waiting for the process to return.
Is there something like exec() that would allow me to keep sending commands to a CLI application?
Please note that since the application is closed-source, I don't have the option of changing the application to look for a lock file or any other thing suggested as answers to similar questions.
You are looking for the proc_open command. This command runs a process connecting its standard input and output to file descriptors opened as pipes in your program. What you write the 0 descriptor is taken as input on stdin by the process, and what it outputs can be read by your program as of from a file. The example code in the linked php documentation should get you going.
However, if you are trying to communicate with mysql specifically, rather use the built in mysql functionality of php
Related
I have searched extensively to solve the issue of opening an external program in phpdesktop without PHP waiting ultimately making PHP timeout.
I need to be able to launch the program with exec() and have the rest of the PHP code execute without waiting on the exec() command. I have tried multiple solutions. To make matters even more complicated the file that is being opened is on a networked drive. Here is what I have tried but has not worked
pclose(popen('start' .$File. '>NUL 2>NUL"', 'r')); This didn't work because the drive is on the network
exec($File); Doesn't work as it waits instead of executing the remainder of the code
system($File); Doesn't work. Same result as exec()
exec($File > /dev/null); Obviously doesnt work because php is on windows
The file being executed is a video file: mp4, avi or mkv. So it's opening the external video player file but like I said above PHP ultimately times out and gives an error after 30 seconds without executing the rest of the code. I just need PHP to ignore the program it opened and go on about its tasks. Any help would be greatly appreciated.
You could use something like "nircmd" which is a windows command-line utility to perform tasks and is a perfect fit for php-desktop. You can use the variety of "exec" actions "nircmd" has, so you could call whatever you want and immediately return to php. See the manual (.chm archive), under windows 7/10 you might have to "unblock" that help file (at file properties), to view the contents.
If 'phpdesktop' uses the built in web server in php (php -S) then it is as far as I know not possible to do so without having the exec call blocking, same with proc_open etc
I looked it up and indeed phpdesktop uses the builtin web server:
https://github.com/cztomczak/phpdesktop/blob/c00988f69348b73b6dee27bdf45d145b719e2a3d/phpdesktop-chrome/php_server.cpp
In theory proc_open should work, but it doesn't
I have a scraper which scrape one site (Written in python). While scraping the site, that print lines which are about to write in CSV. Scraper has been written in Python and now I want to execute it via PHP code. My question is
how can I print each line which is getting printed by python code.
I have used exec function but it is none of my use and gives output after executing all the program. So;
Is it possible to get python output printed while it is getting executed via PHP.
If i understand it well, your python scraper output to a file and you want to "live" display the output via php. What about doing and loop in php in which you use filemtime to know whether or not the file has been updated? You might add a little sleep in order not to overload your server.
If your are using a web page, you may use AJAX to reload only the concerned part of the page at a regular interval.
Hoping this helps you.
Simple case
Assuming execution of scraper is limited to php runtime, run it via popen: http://php.net/manual/en/function.popen.php
More involved case
If you want scraper to run on background and only connect to it vis php from time to time, you can either use some pub/sub toolkit or implement a small web server in the scraper that you can fetch result updates with fopen("https://localhost:port/...") or curl. Many other rpc mechanisms are possible, domain sockets, watching a file, sysv rpc...
I'd communicate using stdout instead of a file. Meaning the python script writes to stdout and the php script reads that.
Using proc_open you can control the python process from php and also read it's output.
Instead of using exec you could use passthru, that will output the data directly to the browser. http://php.net/manual/en/function.passthru.php
That should be enough to get the println from your script.
I think I have a fair idea of what you are saying put I am not too sure what you mean.
If you mean to say that everytime the python script does a print, you want the php code to output what was print?
If that is the case you could pass it as a POST DATA via HTTP. That is instead of printing in Python, you could send it to the PHP Script, which on receiving the data would print it.
I am not too sure if this is what you want though.
For proper communication you need to setup any medium, so you can use fifo, through it you can write string in python and read it with php.
For PHP fifo
http://php.net/manual/en/function.posix-mkfifo.php
For Python
http://www.doughellmann.com/PyMOTW/Queue/
Simply use system() instead of exec(). exec() saves all lines of stdout output of the external program into an array, but system() flushes stdout output "as it happens".
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.