grep'ing a variable - why not working


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep'ing a variable - why not working
# 1  
Old 11-29-2011
grep'ing a variable - why not working

Hi all,

Am writing a ksh script where I am looking for processes that has gone defunct and all of which has the same PPID

PID is the variable that I need to match as this is the process ID of the processes that has gone defunct

Am just curious how come the following DOES NOT work?

Code:
ps -ef | grep "defunct" | grep -v grep | awk '{ print $3 }' | grep "^${PID}$"

I got it to work as below.

Code:
ps -ef | grep "defunct" | grep -v grep | awk '{ print $3 }' | grep "^`echo ${PID}`$"

I would have thought that the first command would have work or shouldn't it?
# 2  
Old 11-29-2011
Due to {} brackets
Code:
$ps -ef | grep "defunct" | grep -v grep | awk '{ print $3 }' | grep "^`echo $PID`$"

# 3  
Old 11-29-2011
Quote:
Originally Posted by newbie_01
I would have thought that the first command would have work or shouldn't it?
It should - e.g. this works on ksh 93s+ (RHEL):
Code:
# ps
  PID TTY          TIME CMD
13088 pts/6    00:00:00 ps
20462 pts/6    00:00:00 ksh
# export PID=20462
# ps -ef | awk '{ print $3 }' | grep "^${PID}$"
20462
20462
20462

However, I'm a bit confused about what you're trying to do? All that will get you is a list of (the same) PPID - which you already know...
# 4  
Old 11-29-2011
Depending on your system, you may be able to just do ps -ef $PID

If not, when you're using awk anyway, you might as well use awk to do the entire statement. You can do powerful things with very simple awk statements.
Code:
ps -ef | awk -v PID="$PID" '$3 == PID'

This should print the entire matching line.
# 5  
Old 11-29-2011
There is much variation in the output format for "ps -ef" .

Please post what Operating System and version you have, the Shell (e.g. bash, ksh, sh etc.) and a sample of a few relevant lines from:
Code:
ps -ef|egrep "defunct|PID"|grep -v "grep"

The "egrep" for "PID" is just to get the column headings for this example and nothing to do with your variable called $PID.

I can't see anything wrong with the syntax of the first example you posted. Had you used the reserved variable name $PPID that would give trouble in many shells (notably bash).

The way your script behaves makes me wonder if your version of "ps", "awk" or Shell is changing the value of ${PID} (but not when it is a parameter to a sub-shell).

If you are on Solaris use "nawk" not "awk".

What are you actually trying to do?
# 6  
Old 11-30-2011
Quote:
Originally Posted by CarloM
It should - e.g. this works on ksh 93s+ (RHEL):
Code:
# ps
  PID TTY          TIME CMD
13088 pts/6    00:00:00 ps
20462 pts/6    00:00:00 ksh
# export PID=20462
# ps -ef | awk '{ print $3 }' | grep "^${PID}$"
20462
20462
20462

However, I'm a bit confused about what you're trying to do? All that will get you is a list of (the same) PPID - which you already know...
Hi,

Thanks for your response. What am trying to do is looking for how many defunct processes are from an existing process ID.

We are using Oracle Enterprise Manager (OEM) which is an Oracle product used for monitoring Oracle databases. OEM uses what is called an agent. For some reason, when the agent starts, after a few days, some of the processes that it spawns becomes DEFUNCT when they shouldn't be. All these DEFUNCT processes has their PPID equal to the PID of the agent.

What am trying to do is write a script how many of such processes are DEFUNCT and if I have more than five (5) that means it is going to generate a file handles exhausted error in which case we have to re-start the agent.

Am wanting to grep using "^${PID}$" because sometimes there are some defunct process that are not spawned by the agent.

Example as below:

Code:
  oracle  2493  2277  0   Nov 27 ?       342:05 /opt/oracle/agent10g/bin/emagent

Checking for defunct processes related to the OEM Agent ...

  oracle  7668  2493  0                   0:00 <defunct>
  oracle  3537  2493  0                   0:00 <defunct>
  oracle 17004  2493  0                   0:00 <defunct>
  oracle  3470  2493  0                   0:00 <defunct>

Am trying to exclude defunct processes where PPID = 24935 or something similar which is why am wanting to make "^${PID}$" works. ${PID} is actually the PPID 'coz am using ps -ef | awk '{ print $3 }'

Hope it's clear what am trying to do.

---------- Post updated at 06:04 AM ---------- Previous update was at 05:58 AM ----------

Quote:
Originally Posted by methyl
There is much variation in the output format for "ps -ef" .

Please post what Operating System and version you have, the Shell (e.g. bash, ksh, sh etc.) and a sample of a few relevant lines from:
Code:
ps -ef|egrep "defunct|PID"|grep -v "grep"

The "egrep" for "PID" is just to get the column headings for this example and nothing to do with your variable called $PID.

I can't see anything wrong with the syntax of the first example you posted. Had you used the reserved variable name $PPID that would give trouble in many shells (notably bash).

The way your script behaves makes me wonder if your version of "ps", "awk" or Shell is changing the value of ${PID} (but not when it is a parameter to a sub-shell).

If you are on Solaris use "nawk" not "awk".

What are you actually trying to do?
Hi,

Thanks for your response. What am trying to do is looking for how many defunct processes are from an existing process ID.

We are using Oracle Enterprise Manager (OEM) which is an Oracle product used for monitoring Oracle databases. OEM uses what is called an agent. For some reason, when the agent starts, after a few days, some of the processes that it spawns becomes DEFUNCT when they shouldn't be. All these DEFUNCT processes has their PPID equal to the PID of the agent.

What am trying to do is write a script how many of such processes are DEFUNCT and if I have more than five (5) that means it is going to generate a file handles exhausted error in which case we have to re-start the agent.

Am wanting to grep using "^${PID}$" because sometimes there are some defunct process that are not spawned by the agent.

Example as below:

Code:
oracle  2493  2277  0   Nov 27 ?       342:05 /opt/oracle/agent10g/bin/emagent

Checking for defunct processes related to the OEM Agent ...

  oracle  7668  2493  0                   0:00 <defunct>
  oracle  3537  2493  0                   0:00 <defunct>
  oracle 17004  2493  0                   0:00 <defunct>
  oracle  3470  2493  0                   0:00 <defunct> 
  
Am trying to exclude defunct processes  where PPID = 24935 or something similar which is why am wanting to make  "^${PID}$" works. ${PID} is actually the PPID 'coz am using ps -ef |  awk '{ print $3 }'

BTW, didn't know there is a reserved variable named PPID.

Hope it's clear what am trying to do.
# 7  
Old 11-30-2011
Please post what Operating System and version you have, the Shell (e.g. bash, ksh, sh etc.).

In most Shells $PPID is the PID of the process which invoked the current Shell. In bash Shell this environment variable is write protected.

Oracle needs substantial unix kernel tuning. The kernel parameter "maximum number of files open by any one user" is as important as "maximum number of files open on the system" and "maximum number of files locked on the system".
There are detailed kernel tuning guides from Oracle for each O/S, but there will still be more tuning depending on the application.

Getting "defunct" processes from an Oracle client-server application is nothing unusal and the Operating System should clear them up within an hour or so. They are commonly caused by people clicking "X" in a Windows client instead of doing a proper application logout. If they persist for longer this can be because of an application error or because they are stuck when accessing broken hardware.
Depending on your circumstances you may need to tune the unix kernel to allow for as many as double the Oracle clients you actually expect ... or more.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep with variable not working

Hello, I am using below code : export ORAR=orp712z.int.thomsonreuters.com #echo $ORAR; if echo $ORAR|grep -i "_" then ORACLE_SID1= echo $ORAR|cut -f2 -d "_" echo $ORACLE_SID1 ORACLE_SID=fgrep "$ORACLE_SID1" /etc/oratab|cut -f1 -d ":" #echo $ORACLE_SID else ORACLE_SID1= echo $ORAR|cut... (1 Reply)
Discussion started by: admin_db
1 Replies

2. Shell Programming and Scripting

Variable not working in grep from script

Hi, If I hard code a value in the grep it works fine from script, when I use variable it doesn't work. On a seperate note, some lines (during testing) works fine from command line but not from scirpt. #!/bin/bash # Will fetch the (oldest - as ls will sort by name by default)Date in the... (7 Replies)
Discussion started by: krish.m
7 Replies

3. Shell Programming and Scripting

Grep'ing information from a log file on SUN OS 5

Hi Guys, I'm trying to write an script that will be launched by a user. The script will look at a log file and check for alerts with the date (supplied by user) and a machine's hostname (also supplied by the user). I'm trying to get the output formatted just like the log file. The logfile looks... (5 Replies)
Discussion started by: illgetit
5 Replies

4. Shell Programming and Scripting

grep'ing dot history file

Hi, I tried to grep ".sh_history" (DOTsh_history) file and did not return anything though I found the word in .sh _history file through vi editor in Linux. Then I tried to grep ".profile" to check if it is the prob with hidden files and I got results. Then I verified the same with my friend... (4 Replies)
Discussion started by: bobbygsk
4 Replies

5. Shell Programming and Scripting

grep'ing a variable that contains a metacharacter ($) with a while loop

This is driving me crazy, and I'm hoping someone can help me out with this. I'm trying to do a simple while loop to go through a log file. I'm pulling out all of the lines with a specific log line, getting an ID from that line, and once I have a list of IDs I want to loop back through the log and... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. Shell Programming and Scripting

pipe'ing grep output to awk

This script is supposed to find out if tomcat is running or not. #!/bin/sh if netstat -a | grep `grep ${1}: /tomcat/bases | awk -F: '{print $3}'` > /dev/null then echo Tomcat for $1 running else echo Tomcat for $1 NOT running fi the /tomcat/bases is a file that... (2 Replies)
Discussion started by: ziggy25
2 Replies

7. Shell Programming and Scripting

grep'ing a file until a certain message appears

Hello, I'm writing a script that will automate the launch of some services on my AIX machine. However, some services are dependent on the successful startup of others. When I start these services manually, I usually just check a log file until I see a message that confirms a successful... (3 Replies)
Discussion started by: pallak7
3 Replies

8. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

9. Shell Programming and Scripting

grep'ing for specific directories, and using the output to move files

Hello, this is probably another really simple tasks for most of you gurus, however I am trying to make a script which takes an input, greps a specific file for that input, prints back to screen the results (which are directory names) and then be able to use the directory names to move files.... (1 Reply)
Discussion started by: JayC89
1 Replies

10. UNIX for Dummies Questions & Answers

grep'ing for text within a bunch of files...?

I have, say, a dozen files, and I want to grep for a string of text within them. I don't remember the exact syntax, but let me give it a shot and show you an idea here... find . -type f -exec grep thisword {} \; ...and there's a way to put more than one grep into the statement, so it will tell... (1 Reply)
Discussion started by: kitykity
1 Replies
Login or Register to Ask a Question