Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:00:19+00:00 2026-06-14T04:00:19+00:00

I can’t seem to get this working. So I’m attempting to write a script

  • 0

I can’t seem to get this working.

So I’m attempting to write a script for my game that connects to the server passing a few parameters for game name/type/players/etc. and the server script then sets up a new instance of the game server as it needs, runs the new instance of the server, finds which port it’s running on, and sends all that info back to the game.

I’ve gotten up to the part where the script needs to execute the server game exe, and get it’s PID (so I can get which port it’s using, etc).

The server script is a PHP script, which is to execute the new instance of the server. (PHP seemed like a good choice, and is relatively in my knowledge).


The script must be able to finish executing, while keeping the game server running in the background.

Being able to run multiple game servers simultaneously is a necessity.


I’ve tried using proc_open but it gives the wrong PID of the process, which makes it useless. (though, it does execute the program right)

<?php

    $startDir = "C:/Torque/My Projects/Grim/game";
    $runExe = "$startDir/Grimwood.exe";
    $envars = " -dedicated -mission \"core/levels/Empty Terrain.mis\"";

    $runPath = $runExe . $envars;

    chdir($startDir);

    $descriptorspec = array(
        0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
        1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
    );

    $prog = proc_open($runPath, $descriptorspec, $pipes, $startDir, NULL);

    sleep(4);

    if (is_resource($prog)) 
    {
        $stats = proc_get_status($prog);
        $pid = $stats['pid'];

        $task = shell_exec("tasklist /fi \"PID eq $pid\"");

        echo("\nProcess: \n$task"); // echos cmd.exe, not process ran
    }

?>

The PID it prints belongs to the console window it’s executing it from, and not the program it actually executed.


Next I tried using the MS script host WScript.Shell, and its exec/run methods.

<?php

    $startDir = "C:/Torque/My Projects/Grim/game";
    $runExe = "$startDir/Grimwood.exe";
    $envars = " -dedicated -mission \"core/levels/Empty Terrain.mis\"";

    $runPath = $runExe . $envars;

    chdir($startDir);    

    try
    {
        $WshShell = new COM("WScript.Shell");

        // gets right process ID
        // but process doesn't start properly with params
        $oExec = $WshShell->exec($runPath);
        echo("PID: " . $oExec->ProcessId);

        sleep(4);

        // executes properly
        // but doesn't return PID
        $oExec = $WshShell->run($runPath);
        echo("PID: " . $oExec->ProcessId);

    }
    catch (Exception $e)
    {
        // never gets hit
        echo("Failed to execute");
    }

?>

Running the exec command, it gives the right PID, but the program doesn’t start properly (just shows a blank terminal window, doing nothing).

Running the run command, it start the program correctly (shows all the text it’s supposed to in the terminal window), though, the command doesn’t return the PID.


Any help on the matter would be greatly appreciated. I’m not sure what I’m doing wrong with the WScript.Shell->exec command for it to not be executing right, so all help on that matter would be appreciated.

If it’s possible, I would like to use the proc_open method as it’s a bit more cross-platform if we ever want to move any server stuff over to *nix.

Thanks for your time, I hope some bright mind can point me in the right direction.


PHP Version: 5.4.3

Dev Machine: Windows 7 Ultimate with SP1

Server Machine: Windows Server 2008 R2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:00:20+00:00Added an answer on June 14, 2026 at 4:00 am

    I managed to get it. I used proc_open to execute, which returns the PPID of the game server ran (the cmd.exe executable).

    As start /b exit’s the cmd.exe once it has executed the program, the only thing that should have that PPID is the game server, thus the rest of it to find the PID was relatively easy.

    This also works when multiple instances are made at once, and when multiple instances are already running.

    This is the end script to get the PID with proc_open, if anyone else wonders across this thread and may happen to need it:

    <?php
    
        $startDir = "C:/Torque/My Projects/Grim/game";
        $runExe   = "$startDir/Grimwood.exe";
        $envars   = " -dedicated -mission \"core/levels/Empty Terrain.mis\"";
    
        $runPath = $runExe . $envars;
    
        chdir($startDir);
    
        $descriptorspec = array (
            0 => array("pipe", "r"),
            1 => array("pipe", "w"),
        );
    
        if ( is_resource( $prog = proc_open("start /b " . $runPath, $descriptorspec, $pipes, $startDir, NULL) ) )
        {
            $ppid = proc_get_status($prog)['pid'];
        }
        else
        {
            echo("Failed to execute!");
            exit();
        }
    
        $output = array_filter(explode(" ", shell_exec("wmic process get parentprocessid,processid | find \"$ppid\"")));
        array_pop($output);
        $pid = end($output);
    
        echo("\nProcess ID: $pid ; Parent's Process ID: $ppid\n");
    
        // returns right process
        $task = shell_exec("tasklist /fi \"PID eq $pid\"");
        echo("\n\nProcess: \n$task\n\n");
    
        // returns no process found
        $task = shell_exec("tasklist /fi \"PID eq $ppid\"");
        echo("\n\nParent Process: \n$task\n\n");
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone maybe tell me why this is not working? I have used echo
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can find why i get this error can someone help? package Android.data; public class
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
Can anyone explain to me why this program: for(float i = -1; i <
Can I run this in a Windows command prompt like I can run it
Can I call select before recv_from on a socket that is blocking?
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.