0

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.

1 Answer 1

1

Just pipe the results to grep -v grep like ps -ef | grep -i "websocket" | grep -v grep This will remove the ps record with the grep in it.

5
  • Would you say that this option is as efficient as the Windows tasklist with filtering that I provided in the question?
    – Mic1780
    Sep 11, 2014 at 17:34
  • I think you will find it to be very fast and efficient.
    – JNevill
    Sep 11, 2014 at 17:36
  • Also, I'm totally down with NuTTyX's answer too. Often in Linux there are many ways to skin the cat.
    – JNevill
    Sep 11, 2014 at 17:38
  • As you replied first and my answer was almost the same, I'm including as a comment to yours: Since grep starts a new command while listing and have the same search term that it should find, it finds itself and other commands. I recommend to modify your grep to surround any single letter between brackets []: ps -ef | grep -i "[w]ebsocket"
    – NuTTyX
    Sep 11, 2014 at 17:40
  • This worked great. I don't notice a performance hit. Thanks
    – Mic1780
    Sep 11, 2014 at 19:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .