Solved

Testing in a shell script (maybe some sed issues)

Posted on 2004-10-07
6
676 Views
Last Modified: 2010-05-18
I'm trying to determine if a process is running and I'm having some issues with my script.  I'm using the '-z' flag on a test which should return True if the string is not a 0 value.  My thinking is that the  ps-ef command string (example below) through grep will only return a line if that process is running (that part works).  What does not work is the -z test always shows the process as running even if it's not.  I thought maybe there was a newline character in there so that's where I started messing with sed (shown below).
 
if [ -z 'ps -ef | grep "{any process name}" | grep -v "grep" | sed -e "/^ *$/d"' ];then
   echo 'Process not running\n'
else
   echo 'Process running\n'
fi

Unfortunately there's no flags on the executable that I am actually testing that will return a code if it's running (runs as a daemon) so I thought this was the best way.  I'm open to suggestions though.  Eventually this will run every 5 minutes and restart the process if it stops.

Thanks in advance for any help.
0
Comment
Question by:lbussy
  • 3
  • 2
6 Comments
 
LVL 20

Accepted Solution

by:
tfewster earned 250 total points
ID: 12252264
It looks like you have single quotes round the 'ps..../d'  instead of backticks so it's not actually running ps
Try the following:

if [ -z "`ps -ef | grep 'bash' | grep -v 'grep' | sed -e '/^ *$/d' > /tmp/check`" ];then
   echo 'Process not running\n'
else
   echo 'Process running\n'
fi

(The > /tmp/check is just so you can see what the command is actually producin; Remove it when you're finished testing
0
 
LVL 11

Expert Comment

by:griessh
ID: 12252414
The backticks is one thing, the other are the quotes ("...") around the whole string as Tim already showed in his if statement. You can always create a variable

RES=`ps -ef | grep 'bash' | grep -v 'grep'
if [ -z "$RES" ];then
...
fi

which makes it easier to debug :-)
0
 

Author Comment

by:lbussy
ID: 12252415
Them pesk backticks again!  Works now, thanks for the help.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 11

Expert Comment

by:griessh
ID: 12252421
should say:
RES=`ps -ef | grep 'bash' | grep -v 'grep'`
0
 

Author Comment

by:lbussy
ID: 12252443
That works as well griessh, thanks for the alternative.  I had already accepted tfewster's answer before I read yours though.
0
 
LVL 11

Expert Comment

by:griessh
ID: 12252459
No problem, he was first :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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 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…

839 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