PHP: How to track the created process on Linux


On Linux, to create a process and show the process id we use this command:

nohup THE_COMMAND > /dev/null 2>&1 & echo $!

To check the process id, we use this command

ps -p $pid

To stop a process with pid, we can use this command:

kill $pid

Now, we start to code a PHP script to track the created process:

<?php

function processmanage_start($cmd){
    $command = 'nohup '.$cmd.' > /dev/null 2>&1 & echo $!';
    exec($command ,$op);
    return (int)$op[0];
}

function processmanage_status($pid){
    $command = 'ps -p '.$pid;
    exec($command,$op);
    if (!isset($op[1]))return false;
    else return true;
}

function processmanage_stop($pid){
    $command = 'kill '.$pid;
    exec($command);
    if (processmanage_status($pid) == false)return true;
    else return false;
}

Leave a Reply