Solved

Running process

Posted on 2011-03-25
5
415 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
5 Comments
 
LVL 14

Assisted Solution

by:svgmuc
svgmuc earned 200 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 300 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

733 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