Solved

Running process

Posted on 2011-03-25
5
412 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now