Categories
PHP

Manage Running Processes

Do you want to manage running processes on your server? If your answer is yes, This article is for you. You may have heard about Symfony/Process. You can say it’s a replacement for shell_exec and exec functions. So we need to play with Symfony Process to achieve our target. I am assuming that you have already configured your laravel project and you know basic knowledge of laravel. And I am not going to the frontend stuff. So let’s get straight to the target.

Step:1 run the below command into your laravel project to generate a controller

php artisan make:controller ProcessController

Step:2 Create a route where we will see the currently running processes. add the below code to your web.php file

 Route::get('getRunningProcesses', 'App\Http\Controllers\ProcessController@getRunningProcesses')->name('getRunningProcesses');

Step:3 Add the below code to your newly created controller ProcessController

<?php

namespace App\Http\Controllers;

use Symfony\Component\Process\Process;

class ProcessController extends Controller
{
    public function getRunningProcesses()
    {
        $runningTasks = new Process(['ps', '-C', 'php', '-f']);
        $runningTasks->run();
        dd($runningTasks->getOutput());
    }
}

The above program will generate output like this

Now as you can see all currently running processes. Now let’s imagine you want to terminate a process. In the above image, each process has a unique process id under the column PID you can use that process id to terminate a specific process. Let’s assume you want to a process with the id 91991. You would do like this

Step:4 Add another route to your web.php file. Add the following code to your route file.

Route::delete('terminateProcessByPID/{pid}', 'App\Http\Controllers\ProcessController@terminateProcessByPID')->name('terminateProcessByPID');

Step:5 Add the following method to your controller ProcessController

    public function terminateProcessByPID(\Illuminate\Http\Request $request,$pid)
    {
        $process = new Process(['kill', '-9',$pid]);
        $process->run();
    }

Warning: Strogly recommended to not to perform this action without knowing what you really want to do.

As you can see the terminateProcessByPID route expects a parameter pid and you just need to pass PID of the specific process, you want to terminate. In our case the PID is 91991. So you have to make the request on the URL /terminateProcessByPID/91991 using the delete method. And the above code will terminate the process 919991.

For some reason let assume you want to teminate all processes of a specific user. If you remember the image above in which there were all running processes there is a column UID. It contains user id "UNIX ID" of the users. So you can use a UID to terminate all processes having this UID. To do that you would do like this

Step:6 Add another route to your web.php file. Add the following code to your route file.

Route::delete('terminateProcessByUID/{uid}', 'App\Http\Controllers\ProcessController@terminateProcessesByUID')->name('terminateProcessesByUID');

Step:7 Add the following method to your controller ProcessController

public function terminateProcessesByUID(\Illuminate\Http\Request $request,$uid)
    {
        $process = new Process(['pkill', '-U',$uid]);
        $process->run();
    }

Warning: Strogly recommended to not to perform this action without knowing what you really want to do.

As you can see the terminateProcessesByUID route expects a parameter uid and you just need to pass UID of a specific user, to terminate all his running processes. Let say UID is fivea So you have to make the request on the URL /terminateProcessesByPID/fivea using the delete method. And the above code will terminate all processes having fivea UID. Isn’t so easy? Let’s try it.

Leave a Reply

Your email address will not be published. Required fields are marked *