skip to Main Content

I am actually working on a big project using php laravel as backend and react as frontend, in which Its like I would upload a zip file containing react project and once the file is uploaded it would extract the file, execute the npm start command to start the server and then use the puppeteer to take a screenshot of the live project..

the problem is that whenever the npm start command is executed and the server is started, the code on the next line wouldn’t get executed , it waits for the npm to finish its job or if would kill it manually from the task manager it would then react to the next line of code, now i can’t post the whole code of the project here so i am going to drop a simple example :

`<?php
// Execute npm start command in the background

mkdir('hello');
// Start Node.js server in the background using cmd.exe
exec('start /B cmd.exe /C npm start');
mkdir('hi');
?>`

in the above example when i use the postman to make a get request first the "hello directory" is created , then the react server is started , but the "hi directory" was never created untill i kill the node proccess from the task manager

so far i tired like everything and my question is, that is it not possible that it would make the hello directory first then start server in the background and then finally make the hi directory ?? Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    the following script worked :

    <?php
    // Function to check if a process is running on a given port
    function isProcessRunning($port) {
        $output = shell_exec("netstat -ano | findstr LISTENING | findstr :$port");
        return !empty($output);
    }
    
    // Function to kill a process running on a given port
    function killProcess($port) {
        $output = shell_exec("netstat -ano | findstr LISTENING | findstr :$port");
        $lines = explode("n", $output);
        foreach ($lines as $line) {
            if (preg_match('/s+d+s+TCPs+d+.d+.d+.d+:' . $port . 's+d+.d+.d+.d+:0s+LISTENINGs+(d+)/', $line, $matches)) {
                $pid = trim($matches[1]);
                exec("taskkill /F /PID $pid");
                return true;
            }
        }
        return false;
    }
    
    // Execute mkdir command for hello directory
    $resultHello = mkdir('hello');
    
    // Execute npm start command in the background
    $command = 'start /B cmd /c "npm start"';
    exec($command);
    
    // Wait for the React server to start (up to 60 seconds)
    $max_attempts = 12; // Total attempts (each attempt waits for 5 seconds)
    $attempts = 0;
    while (!isProcessRunning(3000) && $attempts < $max_attempts) {
        sleep(5); // Wait for 5 seconds
        $attempts++;
    }
    
    // If the React server is running, execute mkdir command for hi directory
    if (isProcessRunning(3000)) {
        $resultHi = mkdir('hi');
        if (!$resultHi) {
            echo "Error: Failed to create the 'hi' directory.";
        }
        // Kill the React server process running on port 3000
        killProcess(3000);
        // Kill the Node.js runtime process
        exec("taskkill /F /IM node.exe");
    } else {
        // If the React server didn't start within the specified time, display an error message
        echo "Error: React server did not start within the specified time.";
    }
    ?>
    

    thanks @S.Imp for giving me the idea.


  2. On a linux or unix machine, you can add an ampersand (&) after the command to ‘background’ it. Instead of this command

    exec('start /B cmd.exe /C npm start');
    

    Add an ampersand:

    exec('start /B cmd.exe /C npm start &');
    

    NOTE, however, that the command will return immediately and the next line of code could be executed before the NPM command has finished. You may need to add some kind of loop to check if it has completed somehow:

    while (!npm_has_finished()) // you'd need to write this function
    {
      usleep(100000); // sleep for 100 milliseconds
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search