简体   繁体   中英

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 commands - 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM