I have a couple lines in PHP to check if a specific process is currently running on my server. If the process is running, I include a script that is used in to interact with said process. Below is the code
ob_start(NULL, 0, PHP_OUTPUT_HANDLER_CLEANABLE);
system('tasklist /FI "IMAGENAME eq websocket.exe" /FO TABLE /NH');
$return = ob_get_clean();
if (!($return === false || stripos($return, 'websocket') === false)) {
$hData['Script'][] = 'websocket.js';
}//END IF
Currently the system call is only usable in Windows because tasklist
is a Windows specific command. Since I am still learning Ubuntu and Unix I cannot think of a way to get a similar output to my Windows command. I've thought about using ps
to check for my process but I don't know how to refine the results based on process name.
Is there a command in Ubuntu that is similar to my Windows command tasklist /FI "IMAGENAME eq websocket.exe" /FO TABLE /NH
that will allow me to filter out non matching processes? The goal is to make it efficient for both Ubuntu and PHP. So filtering the results as much as possible is recommended
Extra Information
I am planning on using this to check if my websocket server is running and if it is include the script so users can use it.
I tried doing ps -ef | grep -i "websocket"
but it would return grep --color=auto -i websocket
which would match the criteria for PHP and include the script. I would like to try to avoid that path.