开发者

What is the best way to work with console application runned in background in php?

I have task to write some "control panel" on php, to work with linux console-based application, game server. So, i need to start, stop (no problem there), and work with console of application - send string commands to it, and catch an output.

What is the best way to do it? I think about creating some .sh scripts/templates, and call it with php shell_exec comman开发者_如何转开发ds - it will be the simplest way. But how to implement work with console - what is the best way to send commands and catch an output? Call "fg" before running script and "bg" after? Use screens and switch to it?

What is the best way to do this?


The best way would be to run the "other application" as a daemon in background, and communicate with it via socket (tcp, unix, ..).

This could be done quite easily if you actually develop the console-based application too. If you don't, and that application is only reading/writing from stdin/stdout, you could build some other controller process that exposes these two I/O channels on a socket.

The first idea I tough about this would be a Python script that uses the subprocess module to run the other process in background and then communicate with it. Then, make the Python script open a socket to dialogate with the PHP application (of course you should be able to do it in your favourite language. Mine is Python :)).


I just got a crazy idea. I can make named pipe - "mkfifo pipename", and then, work with it from shell_exec (for example I can do "echo quit >> pipename".

Problem is that I can't catch an answer of specifically last command, but it would be too cool. In this stage of project i just need to send commands to server and catch console output. I will think some more time, but it seems using named pipes is the easyest solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜