0

I will try to summarize my problem in order to make it understandable.

I have a script serverHandler.php that can start multiples server using an another script server.php. So I start a new server like this :

$server = shell_exec("php server.php");

So now I will have a server.php script that will run in the backgroung until I manually kill it.

Is there a way to directly manage the kill of this server within the script serverHandler.php like that ?

// Start the script server.php
$server = shell_exec("php server.php");

// Stop the script that run on background
// So the server will be stopped
killTask($server);
1
  • You can execute a command to kill the task as you do manually, but I guess you'll need the ID to kill it. If your server script is running in a loop, maybe you can store a variable (file or database) to tell the script to stop and just exit; Commented Dec 4, 2019 at 17:55

1 Answer 1

1

Shell management of tasks is typically done using the ID of a process (PID). In order to kill the process, you must keep track of this PID and then provide it to your kill command. If your serverHandler is a command line script then keeping a local copy of the PID could suffice, but in a web interface over HTTP/HTTPS you would need to send back the PID so it could be managed.

Using a stateless language like PHP for this is not recommended, however, as attempting to retrieve process information, determine whether or not the process is one of the server processes previously dispatched, and other fine little details will be unnecessarily complicated and, if you're not careful, error-prone and potentially even dangerous.

Better would be to use a stateful language like Java or Python for managing these processes. By using a single point of access with a maintained state, you can have several threads "waiting" on these processes so that :

  • you know for certain which PIDs are expected to be valid at all times,
  • you can avoid the need for excessive PID validation,
  • you minimize the security risks of bad PID validation,
  • you know if these processes end prematurely so you can remove them from the list of expected processes automatically
  • you can keep track of which PID is associated with which server instance.

Use the right tools for the problem you're trying to solve. PHP really isn't the tool for this particular problem (your servers can be written in PHP, but use a different language for your serverHandler to avoid headaches).

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

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.