Solved

Testing in a shell script (maybe some sed issues)

Posted on 2004-10-07
6
674 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Shell script errors 10 113
FTP on FreeBSD server 2 111
Skill Development 15 163
NTP Server in VMware 5 164
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

912 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now