Jump to content

Some help needed with executing one PHP file from another.


yuvallahav

Recommended Posts

Hello all  ;D

 

To make a very long story short, I will give you the basics of what I did, and what is my problem, and I hope some one here could help me solve it!

 

So I've made this script, that holds a collection of socket, for an online multiplayers game, which work (pretty much) without problems.

The problem I am facing here is that every once in a while, my script gives out and die, which is not the problem, since I know why it does that, and it's beeing taken care of, but for just these cases, I have made another script, on a different PHP page, which trys to connect to my server via socket, and if it all goes well, it starts the count down again, if the connection could not be astablished, I want to re-initialize my original PHP scrip.

To make this clearer, lets think of 3 files... server.php which holds the sockets, client.php, which holds the client (made in flash + xml socket), and servercheck.php, which has a loop that checks the connection to the server every 30 seconds.

The problem I am facing is the re-launching of the server.php file.

It holds the socket collection, inside a while loop that never ends (in a perfect world), always checking and handling the connections, and to initialize this file when starting to work with it, I simply open it with my browser (when it's located on the server...), then close the browser, while the script keeps on running on the server untill something kills it.

When something kills my script, and my "servercheck.php" loop can't connect to it anymore, I need to execute some command, some function, that will have the same effect as actually putting the path to the "server.php" file in the browser's address line, and hitting "go".

 

I have tried "exec", using the path to the server script, but it hangs the PHP, read about it, tried to do it with some special functions that are supposed to make the "program" run in the background, didn't work too, I have tried "include" and his friends (once, and such), still no good, since I need my server script to run indipendetly, and not loaded into another file and be linked to it liake that.

 

Any ideas? something I'm missing here?

 

Thanks,

 

Yuval Lahav.

Are you running Linux for the server by any chance? If so, you can pass a command like this to exec() and it won't hang PHP:

 

exec("/path/to/server.php > /path/to/server.log &");

/path/to/server.php is obviously the path to the script

> /path/to/server.log will direct all output from the script (if you don't care about the output, use > /dev/null instead)

& will tell linux to send the command to the background

 

again...this will not work on windows. i too have searched for a windows option and have not found one yet....

Hiya,

Well, my server is Linux, but alas, my local machine is windows, which I use for testing...

So I actualy tried what you wrote here, but it didn't work on my windows machine, I didn't even think to test on the actual server (since we have many other sites on that server, I wouldn't want a php hangup to kill all our sites), but I guess I will have to try it out and see what happens... thanks, and I will let you know how it came around.

 

Yuval Lahav.

If you want to do a simple test, create a 'server' file that has a sleep(30) or something in it, and then a 'servercheck' file that runs the exec command. When you execute the 'servercheck' file it should finish right away. That is safe to test on a production server.

Ok... I must be doing something wrong... maybe it's my fault, maybe it's the server's...

 

What I get when I try to run 'exec("home/xxxxxx/socket_test/server.php > /dev/null &");' is.... nothing at all!

It executes the command, but first, I don't get any error messages from it (which is a good sign, no?), but, also, it has no effect at all.. and I know this, since on activation of the server file (server.php) writes a line to a txt file saying something like "application intiated at 12:00", and I get nothing of the sort, and ofcourse, the server script being a controller for sockets, and I still can't connect to it after trying to "re initialize" it, meaning, again, not working... any idea what went wrong?

sorry...forgot one piece...you probably need PHP in front of the script...

 

exec("/path/to/php -f home/xxxxxx/socket_test/server.php > /dev/null &");

 

if you don't know the path to PHP, you should just be able to run 'which php' from the command line

 

 

EDIT: And you won't get any error messages, etc. If you want a good way to check if the server is up, you can also use something like flock() to lock a file. Then, in servertest, if you can lock the file, the server is down. After you run the exec, check that file again to make sure the server is up.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.