Solved

Testing in a shell script (maybe some sed issues)

Posted on 2004-10-07
6
673 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
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.

707 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