Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

Running process

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
Mr_Oz
Asked:
Mr_Oz
2 Solutions
 
svgmucCommented:
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
 
woolmilkporcCommented:
wc -l always returns true!

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

wmp
0
 
woolmilkporcCommented:
... and "grep -q" will make ">/dev/null" unnecessary.

if ps -ef | grep -v grep | grep -q myprocess
  then ...
(process running)
0
 
Mr_OzAuthor Commented:
Thanks guys
0
 
TintinCommented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now