?
Solved

Testing in a shell script (maybe some sed issues)

Posted on 2004-10-07
6
Medium Priority
?
679 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 1000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

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 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 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.
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

801 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