[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Running process

Posted on 2011-03-25
5
Medium Priority
?
421 Views
Last Modified: 2013-12-26
I have a real simple shell script it is telling me the process is running even when it is not.  What am I doing wrong?
if ps -ef | grep myprocess | grep -v grep | wc -l > /dev/null
then
	echo "process running"
else
	echo "process not running"
fi

Open in new window

0
Comment
Question by:Mr_Oz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Assisted Solution

by:svgmuc
svgmuc earned 800 total points
ID: 35218004
I'd use test

if test `ps -ef | grep myprocess | grep -v grep | wc -l` = 0; then
    echo "process not running"
else
    echo "process running"
fi

alternatively, you can use [ ]

if [ `ps -ef | grep myprocess | grep -v grep | wc -l` = 0]; then
    echo "process not running"
else
    echo "process running"
fi
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35218063
wc -l always returns true!

Better use
if ps -ef | grep -v grep | grep -c myprocess >/dev/null
  then ...

wmp
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1200 total points
ID: 35218099
... and "grep -q" will make ">/dev/null" unnecessary.

if ps -ef | grep -v grep | grep -q myprocess
  then ...
(process running)
0
 
LVL 1

Author Closing Comment

by:Mr_Oz
ID: 35218433
Thanks guys
0
 
LVL 48

Expert Comment

by:Tintin
ID: 35219681
If you have pgrep, then a simple

if pgrep -x process
then
   echo process running
fi
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question