By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,743 Members | 804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,743 IT Pros & Developers. It's quick & easy.

Running php file as a background process

P: n/a
Hi to all,
There is a start button in my page. if user clicks on that then a php
program should start and should listen on a particular port. and also
user should able to do other tasks on that page.
Is there any way to run php file as background process. Or is there
any to run multi thread in php.

Regards,
Ravindra.Y

Nov 6 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Greetings, Ravi.
In reply to Your message dated Tuesday, November 6, 2007, 13:12:05,
There is a start button in my page. if user clicks on that then a php
program should start and should listen on a particular port. and also
user should able to do other tasks on that page.
Is there any way to run php file as background process. Or is there
any to run multi thread in php.
Ask Your hosting company support staff. Running processes as daemons are
denied by most providers.

Otherwise, if it is Your own computer, You're trying to solve problem
backwardly. Why not write script that listen to specific port and keep
it running in background all day?
(Yes, "listen" is a key word to search through manual)
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Nov 6 '07 #2

P: n/a
Hi,
Thanks for replying. Ya this is my own system. But if it listen any
commands then the handler should execute the functions of a
particular object. where object will be created when i start the
process.

The process is like this

1. user presses a start button.
2. Then i should create an object . For exg obj = new A(); where obj
will have the user details.
3. then one program should run background to listen on a port.
4. When it got any response then it should manipulate the object user
data.
5. In the meanwhile user can use the user interface through browser.

If u got any solution plz send me it is very urgent.

Regards,
Ravindra.

Nov 6 '07 #3

P: n/a
Ravi wrote:
Hi,
Thanks for replying. Ya this is my own system. But if it listen any
commands then the handler should execute the functions of a
particular object. where object will be created when i start the
process.

The process is like this

1. user presses a start button.
2. Then i should create an object . For exg obj = new A(); where obj
will have the user details.
3. then one program should run background to listen on a port.
4. When it got any response then it should manipulate the object user
data.
5. In the meanwhile user can use the user interface through browser.

If u got any solution plz send me it is very urgent.

Regards,
Ravindra.

Ravi,

You can do it in PHP, at least on Unix, but it's not going to be easy.

Probably the best way is with shared memory and semaphores.

First of all, you'll need to compile PHP with the --enable-sysvsem flag
to get the shared memory and ipc functions.

You can then create a shared memory segment and use semaphores to
serialize access to the shared memory, or use message queues to send
messages back and forth (or both). Start a process in the background to
handle the socket communications and communicate between it and your web
page.

But there are a lot of problems with this. First of all, HTTP is a
request/response protocol; the server can't just arbitrarily send
information to the browser. The browser has to request it. So if there
is a change in the object's data, this won't be reflected in the user's
browser until they refresh (or do something else). You can get around
this to a certain point with automatically refreshing the page (i.e.
meta refresh or javascript), but there will be a delay.

Also, web pages are transactional - that is, from the server side, the
page loads, executes, sends its data to the client and terminates. So
you need to keep track of your communications methods between pages.
You can use sessions to do this, but you won't be able to keep the
shared memory or message queue itself in the session - just the name.

Something like this isn't real difficult in C/C++ or Java (but it's not
easy, either). However, I really wonder if PHP is a good choice here.
I would think maybe a Java applet on the client and a Java program
running on the server would be better.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 6 '07 #4

P: n/a
On Nov 6, 1:33 pm, Ravi <Ravindrayep...@gmail.comwrote:
Hi,
Thanks for replying. Ya this is my own system. But if it listen any
commands then the handler should execute the functions of a
particular object. where object will be created when i start the
process.

The process is like this

1. user presses a start button.
2. Then i should create an object . For exg obj = new A(); where obj
will have the user details.
3. then one program should run background to listen on a port.
4. When it got any response then it should manipulate the object user
data.
5. In the meanwhile user can use the user interface through browser.

If u got any solution plz send me it is very urgent.

Regards,
Ravindra.
Consider what you were already advised - a daemon program resident in
memory all the time, not on user request. You can also consider using
AJAX on the client side, since going away from the page is equal to
losing every connection with the server for a moment, which is enough
for the server to "forget" about your client, unless some form of
"key" is included in the cookies etc. by which the server can
recognize the client from before.

I think, however, that if you're in a "hurry", that you better
reconsider the whole thing.

Nov 6 '07 #5

P: n/a
Rob
On Nov 6, 10:12 am, Ravi <Ravindrayep...@gmail.comwrote:
Hi to all,

There is a start button in my page. if user clicks on that then a php
program should start and should listen on a particular port. and also
user should able to do other tasks on that page.

Is there any way to run php file as background process. Or is there
any to run multi thread in php.

Regards,
Ravindra.Y
Depending on the Operating System, I would use exec() to submit a
'cron' job running a php script to listen to the port.

I would then use a file or database to communicate between the two
applications.

If you are running Windows, you can use 'schtask' instead of cron, but
it depends if your host allows O/S commands or not.

Rob.

Nov 6 '07 #6

P: n/a
Thank you guys for all u r replys.
let me say one thing that after initiating the process i need not to
display it in browser.Actually i am using it to communicate with
Asterisk server.

Initiated process by the user will have some phone no [may be in
thousands] my program should able to repeat the the loop to make
calls. Then when the call connected then Asterisk will raise some
events on one port where i should listen it. After completion of calls
the listening program should stop. Where i am thinking that is it
possible to bind these 2 things into one class to avoid db connection.

I know these things can be done in C++ or Java but all my project is
done in PHP where this is a simple module in that. I appreciate if
anybody advises me something.

Regards,
Ravindr.Y

Nov 7 '07 #7

P: n/a
Ravi wrote:
Thank you guys for all u r replys.
let me say one thing that after initiating the process i need not to
display it in browser.Actually i am using it to communicate with
Asterisk server.

Initiated process by the user will have some phone no [may be in
thousands] my program should able to repeat the the loop to make
calls. Then when the call connected then Asterisk will raise some
events on one port where i should listen it. After completion of calls
the listening program should stop. Where i am thinking that is it
possible to bind these 2 things into one class to avoid db connection.

I know these things can be done in C++ or Java but all my project is
done in PHP where this is a simple module in that. I appreciate if
anybody advises me something.

Regards,
Ravindr.Y

Ravi,

Maybe the rest of your project is being done in PHP, but that doesn't
mean PHP is the best piece for this. You're going to run into a lot of
problems.

I don't know why you don't want to use a database. That would be a
logical way to handle things.

I think you should reexamine your approach to this piece.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 7 '07 #8

P: n/a
On Nov 6, 10:12 am, Ravi <Ravindrayep...@gmail.comwrote:
Hi to all,

There is a start button in my page. if user clicks on that then a php
program should start and should listen on a particular port. and also
user should able to do other tasks on that page.

Is there any way to run php file as background process. Or is there
any to run multi thread in php.

Regards,
Ravindra.Y
Hi Ravi,

Although it may be possible to write a TCP server with PHP, I would
not recommend it. If it's just a simple server you're trying to
write, try using a better-suited language if possible. I would
recommend Ruby, it is excellent for network programming, and it has
good string and file handling.

Hope this helps,

-James.

Nov 8 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.