Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

near real time if script

Status
Not open for further replies.

segment

ISP
Jun 15, 2004
225
US
Hey all, need advice on something...

Have machine A and machine B. Both have to monitor a specific term and react. Its being run from cron (*/1 * * * * /run/this/command)...

Machine B needs to tail -f /var/log/messages for the field "is dead" then react:

tail -f /var/log/messages|grep "is dead" &> /dev/null
if [ "$?" != true ]
then
/run/this/command
fi


Any suggestions?

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Segment - pardon me for asking, but what's the question?

Alan Bennett said:
I don't mind people who aren't what they seem. I just wish they'd make their mind up.
 
Dur... My question was dumb I figured what I needed to out however this is my new question (let me make sure I type it as a question this time)

I'd like to be able to react at the following:

ps -ax|awk '/program_name/{print $5}'|sed -n '1p'

The output of this would be:

# ps -ax|awk '/program_name/{print $5}'|sed -n '1p'
/sbin/program_name

What I'd like to do is run this from cron on an "if" statement basis.

If ps -ax|awk '/program_name/{print $5}'|sed -n '1p' sees program_name do nothing, if it doesn't see something then it needs to run /sbin/program_name

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Hi

Is faster and uses less resources if you use less pipes & programs. You not need to precess the entire list then use [tt]sed[/tt]. If you need one line, then [tt]exit[/tt] from [tt]awk[/tt] after the first match.
Code:
ps -ax|awk '/program_name/{print $5[red];exit[/red]}'
Anyway, I am happy to see that you gave up at least with [tt]tail[/tt]. ;-)

My [tt]ps[/tt] is able to :
man ps said:
-C select by command name
So I can do :
Code:
ps -C program_name || program_name &

Feherke.
 
I can ps -C program_name|awk '{print $4}'|sed -n '2p' to get program_name but I need to watch every x or so minutes and if its running do nothing if not, run program_name. This is where I'm stuck...

Something like:

RUNNING=`ps -C program_name|awk '{print $4}'|sed -n '2p'`

while [ "$RUNNING" != program_name ]

if [ "$RUNNING" -eq program_name ]
then
/sbin/program_name
fi

exit 0

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Hi

Code:
ps -C program_name[red][s]|awk '{print $4}'|sed -n '2p'[/s][/red]
You not need to filter the output of [tt]ps[/tt], because it sets the exit code : if any program with the given name is runnig, then 0, else not 0.

So the only thing you need to do is to execute that program if [tt]ps[/tt] returns non 0 exit code :
Code:
ps -C program_name || program_name &

Feherke.
 
Dur!!!!!!!!!!!!!!!!!!! Feherke you pimp. See maybe I should actually start reading instead of skimming :| *bows*

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Hi

And I should actualy start testing before posting.

Without braces ( {} ) the shell will push the entire line into background :
Code:
ps -C program_name || { program_name & }

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top