Solved

Testing in a shell script (maybe some sed issues)

Posted on 2004-10-07
6
678 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
[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
  • 3
  • 2
6 Comments
 
LVL 21

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
Industry Leaders: 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!

 
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

Industry Leaders: 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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

724 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