Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if process is running

I am trying to check if a process is running. If it is running I want a return value of 'OK' and if not a return value of 'Not OK'. I can only use 'ps' without any other arguments attached (eg. ps -ef) if thats the correct term. The code I have is:

if ps | grep file; then  echo 'OK'; else  echo 'NO'; fi

The problem with this is that it does not search for the exact process and always returns 'OK', I don't want all the information to appear I just want to know if the file exists or not.

like image 670
chrisg Avatar asked Feb 04 '10 14:02

chrisg


People also ask

How do you check if a process is running in the background?

Most people have at least heard of “Ctrl+Alt+Delete”. This key combination allows the user to open Windows Task Manager. Task Manager is a utility within the Windows operating system that shows information such as running processes, computer performance, background services, and more.


2 Answers

Your code always returns 'OK', because grep finds itself in the process list ('grep file' contains the word 'file'). A fix would be to do a `grep -e [f]ile' (-e for regular expression), which doesn't find itself.

like image 67
tangens Avatar answered Sep 23 '22 20:09

tangens


Spare grep for real problems:

ps -C file

avoids the problem of using grep altogether.

like image 42
user unknown Avatar answered Sep 23 '22 20:09

user unknown