bash conditions and process pid / exe tips

98 views
Skip to first unread message

hq4...@gmail.com

unread,
Dec 6, 2006, 3:03:13 PM12/6/06
to bash tips
===
=== Taken from Linux-IL
===

Shachar Shemesh <linu...@shemesh.biz>

Hi all,


I have a shell programing question. I want to write a test that runs a
certain background program if it doesn't already exist. What I want to
test is this:

If the pid file doesn't exist

or

The pid file relates to a non-existing process

or

The process is not what I'm trying to run

then

run the program.


The two conditions are fairly simple to code in bash:

if [ ! -f /var/run/pid -o ! -d /proc/`cat /var/run/pid` ]

then

/usr/bin/program

fi


The third one I'm having some difficulties with. It's fairly easy to
find out whether it's the right program or not. Something along the
lines of:

(readlink /proc/`cat /var/run/pid`/exe | grep -q progname)

will return 0 if it's the right program and 1 if it's not. In fact,
that's exactly my problem. I want the "0" and "1" to be reversed. If I
did:

if [ ! -f /var/run/pid -o ! -d /proc/`cat /var/run/pid` ] || (readlink
/proc/`cat /var/run/pid`/exe | grep -q progname)

then

...


Then the program would get run if the pid file is gone, and it will get
run if the pid file is there but there is no such process, but if there
is such a process (but it's not the right one) it will not get run, and
if there is such a process and it is the right one, the process will be
run again!


Any ideas on how to correctly code this piece of code would be greatly
appreciated.

Shachar

====

Alex Shnitman <alex...@yahoo.com>

> (readlink /proc/`cat /var/run/pid`/exe | grep -q
> progname)
>
> will return 0 if it's the right program and 1 if
> it's not. In fact,
> that's exactly my problem. I want the "0" and "1" to
> be reversed. If I did:

You can add -v to the grep command line, it will
reverse its function. Or replace "grep -q progname"
with "(grep -q progname && exit 1; exit 0)".

--Alex

====

Shachar Shemesh <linu...@shemesh.biz>

Alex Shnitman wrote:

>You can add -v to the grep command line, it will
>reverse its function.
>
It may help this particular case, but -v reverses the search criteria,
not the overall result.

Take a file that has the two lines:
1
2

Doing "grep 1 file" will result in "0" (found), while doing "grep -v 1
file" will also result in "0", as there is a line in the file that does
not contain "1". In any case, the same situation may apply to other
cases as well.

> Or replace "grep -q progname"
>with "(grep -q progname && exit 1; exit 0)".
>
>
That will work, but I was really hoping to avoid such a convulted case.
If I wanted to do that, I could also do:
if ...
then
(readlink .. | grep -q progname) || runcommand
fi

Shachar

===

Nadav Har'El

On Mon, Sep 05, 2005, Shachar Shemesh wrote about "Shell programing
question":
> will return 0 if it's the right program and 1 if it's not. In fact,
> that's exactly my problem. I want the "0" and "1" to be reversed. If I did:

Is that your only program - reversing the return code? This is easy,
just
use the "!" operator!

For example:

$ true; echo $?
0
! true; echo $?
1

Yes, the "!" operator works not just in an if statement.

> Then the program would get run if the pid file is gone, and it will get
> run if the pid file is there but there is no such process, but if there
> is such a process (but it's not the right one) it will not get run, and
> if there is such a process and it is the right one, the process will be
> run again!

I'm not sure what you're saying here. If your if statement doesn't do
the
correct logic, try to fix its logic. Sometimes you might want to do a
couple of ifs, nested, to simlify the logic.

--
Nadav Har'El | Monday, Sep 5 2005, 1
Elul 5765
n...@math.technion.ac.il
|-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |If I were two-faced, would I be
wearing
http://nadav.harel.org.il |this one?.... Abraham Lincoln

===

Amos Shapira

On 9/5/05, Shachar Shemesh <linu...@shemesh.biz> wrote:
> Alex Shnitman wrote:
>
> >You can add -v to the grep command line, it will
> >reverse its function.
> >
> It may help this particular case, but -v reverses the search criteria,
> not the overall result.
>
> Take a file that has the two lines:
> 1
> 2
>
> Doing "grep 1 file" will result in "0" (found), while doing "grep -v 1
> file" will also result in "0", as there is a line in the file that does
> not contain "1". In any case, the same situation may apply to other
> cases as well.

But by definition the input contains only one line - so either it
matches or not.

BTW - I'd suggest using egrep and a very strict regular expression to
avoid false positives.

Cheers,

--Amos

Reply all
Reply to author
Forward
0 new messages