pass variables/values from php to opened window process and vice versa - php

i want to have an constantly opened program (wrote in c++ prefferably).
when the php script is acessed, it will be acessed with some variables which will be passed to the active program. then the program will make some calculations, and it will pass other variables back to the php script, which will be echoed (or they can be echoed from the program too, if it is possible). after the php script ends, the program must be active!
i know that there is the command exec, and i can run a program with those params(which can be variables), but i don't want that since the program must run even if there are no active php script at that time.
i hope you understood my problem.

You can use shared memory functions.

I would suggest using sockets to communicate between the C++ program and the PHP script. Therefore your C++ program would act as a server and the PHP script would connect to it with the Socket functions and localhost address. Then you could send your data between the two programs and when the PHP script ends, the C++ program would stay alive and wait for the next connection.

Related

Communication of my Python program with php

I have written a python which used to take values from command line and process the same. 5 variables are taken using raw_input() and in each case something is returned to screen.
I want this whole interaction to happen via php program which calls my python program. They are supposed to exchange variables more than one time.
I have read the possible solutions like running the python through php via shell and passing arguments. But I am not sure how once I start my python program I can simply keep on sending variables to it so that my python program reaches its logical end by getting variables from php.
you have to use an IPC mechanism like file, pipe, named pipe, shared memory,...
http://en.wikipedia.org/wiki/Inter-process_communication
You can generally communicate between languages by using common
language formats, and using stdin and stdout [pipe] to communicate
the data.
from: http://www.stackoverflow.com/questions/14047979/executing-python-script-in-php-and-exchanging-data-between-the-two

is there a way to launch a php script and get the status?

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.

php web infinite loop - how to stop

I need to write php code to routinely check for the feed of a specific website and send email to users if there is something new. The php code should run forever when it is started. However, my problem is that sometimes, my client want to manually turn off the script without using terminal. Can I stop a php script using another script or what can I do for this case?
Client only give me ftp access to an apache server, so I cannot use some other techniques like Python and Perl or terminal, which I believe should be easier for this task
use a file.
make your script check presence of some file, say, stop.flag in every iteration
if so - stop it

running console app from php script

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.

Start local PHP script w/ local Python script

The Python program I'm writing needs to start a local PHP script outside of Python's process. The program also needs to pass params to the PHP script. So far this seems to start the script:
os.system( path_to_script_here param param )
However, I'm pretty certain that Python remains running until the PHP script is complete.
I've also looked at the various os.spawn methods and I'm not sure which would be appropriate for my case. Any ideas?
Thanks!
See: How to start a background process in Python?

Categories