John Davidson

End Process Created by PHP exec in Ubuntu using Apache

0 comments
Message:


I have an Ubuntu VM running in VirtualBox that hosts a server using Apache. The concept of the server is to accept HTTP POST requests, store them in a MySQL database and then execute a Python script with the relevant POST data to be displayed in a Discord channel.


The process itself is working but each time the PHP script calls the Python script, a new process is created that never actually ends. After a few hours of receiving live data the server runs out of available memory due to the amount of lingering processes. The PHP script has the following exec call as the last line of code;


exec("python3 main.py $DATA");


I would like to come up with a way to actually kill the processes created from this exec command (using user www-data), either in the Python file after the script is executed or automatically with an Apache setting that I probably just do not know about.


When running the following command in a terminal I can see the different processes;


ps -o pid,user,%mem,command ax | sort -b -k3 -r


There are 3 separate processes that show up, 1 referencing the actual python3 exec command as marked up in PHP;



  • 9903 www-data 0.4 python3 main.py DATADATADATADATADATADATA


Then another process showing the more common -k start commands;



  • 9907 www-data 0.1 /usr/sbin/apache2 -k start


And lastly another process very similar to the PHP exec command;



  • 9902 www-data 0.0 sh -c python3 main.py DATADATADATADATADATADATA


How can I ensure Apache cleans these processes up - OR what do I need to add into my Python or PHP code to appropriately exec a Python script without leaving behind processes?



My Answer:
One way to ensure that the processes created by the PHP exec command are properly cleaned up is to use the `proc_open` function instead of `exec`. This function allows you to create a process and obtain a handle for it, which you can then use to close the process when you are done with it.

Here is an example of how you can modify your PHP code to use `proc_open`:

php
$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
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);

$process = proc_open("python3 main.py $DATA", $descriptorspec, $pipes);

if (is_resource($process)) {
// Close the process when you are done with it
proc_close($process);
}


By using `proc_open` and `proc_close`, you can ensure that the processes created by the PHP script are properly cleaned up after they have finished executing.

Additionally, you can also consider adding a signal handler in your Python script to catch the SIGTERM signal and perform any necessary cleanup before the process is terminated. This way, you can ensure that the Python script releases any resources it may be holding onto before it exits.

Overall, by using `proc_open` in PHP and implementing proper cleanup mechanisms in your Python script, you can prevent the accumulation of lingering processes and avoid running out of memory on your server.

Rate this post

5 of 5 based on 9522 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us