0

i have tried opening .bat files in PHP using many syntax and they are working fine. It runs the file and stores the output. But im having a new problem in hand where i need to initiate a server program from PHP.

Lets say i have a the server program "server.java" and a client program "client.java". The server needs to be run first and then the client. I have created a batch file to run this server program as "server.bat". Is it possible to initiate this server.bat file from the PHP side and make it run in the background until the client finish its process?

1
  • Ummm... why do you want to start (and control) the process from PHP? How are the server and client connected? Also, you can't run "server.java" or "client.java" programs like that since Java must be compiled to run. Commented Apr 10, 2014 at 2:34

3 Answers 3

1

You're looking for the exec function.

Be aware of a few dangers with this function, namely:

When allowing user-supplied data to be passed to this function, use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.

Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

Reading Output

The exec command has an optional variable called $output which can be used to display the output of the command run via PHP. However, this only works after the sub-process (in this case the batch file) exits, which does not work for a Server script.

To display information while the script is still running we must redirect the output of the script to a file and then read that file. To demonstrate this, we'll take our example:

exec("server.bat");

and add a redirect to "server_log.txt"

exec("server.bat > server_log.txt");

Once this is running we can check the output of the batch file by reading "server_log.txt" A good (if somewhat dated) tutorial on reading files from PHP can be found here.

Sign up to request clarification or add additional context in comments.

4 Comments

yes i tried the exec() function but i cant see the batch file running in the background
@Mano When you say you can't 'see' it, do you mean you can't capture the batch files output in PHP? Or do you mean you can't see the process running at all?
Both. My server program creates a socket and waits for the client program to execute..I printed a line saying "Server is online" in the main class of the server program but i cant capture that in PHP also..i need to make sure my server is running when i execute the batch file..its working if i double click the .bat i just cant run it in PHP
@Mano I updated my answer to show a way to retrieve the server.bat output while the server is running. Note though, that I have only ever tested this on Linux, not Windows.
0

Try exec("psexec -d server.bat");

Comments

0

I got this to work by executing the following code:

exec("start cmd /c test.bat", $output);
var_dump($output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.