Development

Check if a process is running on Linux with PHP

Check if a process is running on a Linux system such as Ubuntu by using PHP. This is done by using pgrep and checking if its response is empty.

pgrep looks through the currently running processes for a match to the search criteria, the search criteria is what comes after pgrep.

To check for FFmpeg running you would do pgrep FFmpeg and it would return all the process id’s (PID) for instances of FFmpeg running.

echo shell_exec("pgrep youtube-dl");

The above command is if youtube-dl has at least one instance running then the process id/s will be returned.

Now for a check to simply return yes or no upon the process running:

if (empty(trim(shell_exec("pgrep youtube-dl")))) {
    echo "No";
} else {
    echo "Yes";
}

If youtube-dl was not running then the shell_exec would be empty because it is not returning a PID, it returns No in this case.

Here it is put into a function that can be re-used fro different process names i.e FFmpeg, Wget, PHP etc:

function processRunning(string $process): bool
{
    if (empty(trim(shell_exec("pgrep $process")))) {
        return false;
    } else {
        return true;
    }
}

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago