Solved

Running process

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

717 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