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
the following script worked :
thanks @S.Imp for giving me the idea.
On a linux or unix machine, you can add an ampersand (&) after the command to ‘background’ it. Instead of this command
Add an ampersand:
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: