LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   orphan ksh in top output ? (https://www.linuxquestions.org/questions/programming-9/orphan-ksh-in-top-output-4175472231/)

megatron521 08-05-2013 03:27 PM

orphan ksh in top output ?
 
Hi Friends - I am trying to write a script that would check for orphan ksh processes from top command and alert the team.

Here is the sample output of top with Linux flavor

more topCPU.log
%CPU %MEM VSZ RSS USER STIME PID COMMAND
0.0 0.0 10372 560 root Jun10 1 init [5]
0.0 0.0 0 0 root Jun10 2 [migration/0]
0.0 0.0 0 0 mega Jun10 3 ksh
0.0 0.0 0 0 appuser Jun10 4 -ksh
0.0 0.0 0 0 root Jun10 5 -ksh


I wrote a snippet that would basically outputs the entry

Example:
=> more topCPU.log | awk '{print $8}' | grep "^-ksh$"
-ksh
-ksh

How could I get entire line ? OR any better approach ?

Thanks

unSpawn 08-05-2013 06:53 PM

Code:

\ps -o ppid,args -C -ksh|awk '{if ($1 == 1) {print }}'
or
Code:

pgrep -P 1 -f -- "-ksh"
?

megatron521 08-06-2013 09:43 AM

Hi UnSpawn -thanks for your reply

I dont want to find out through ps/pgrep commands, coz we have several hosts and end up running several instances of script

Just want to grep from log files which have entries of ksh processes

megatron521 08-06-2013 09:50 AM

Code:

more topCPU.log
=>
%CPU %MEM    VSZ    RSS      USER        STIME    PID COMMAND
 3.9  0.0  415148    7032      root        Jun10    6392 bin/dna -server.host
 0.0  0.0  10372    560      root        Jun10      1 /bin/ksh /apps/sa/tools/monitor.ksh
 0.0  0.0      0      0      mega        Jun10      2 /bin/ksh /apps/sa/tools/abc.ksh
 0.0  0.0      0      0      user        Jun10      3 ksh
 0.0  0.0      0      0      root        Jun10      4 [watchdog/0]
 0.0  0.0      0      0      root        Jun10      5 [migration/1]
 0.0  0.0      0      0      root        Jun10      6 [ksoftirqd/1]
 0.0  0.0      0      0      mega        Jun10      7 ksh

when I grep it, its getting all ksh processes instead of just ksh or -ksh

Desired output:
0.0 0.0 0 0 user Jun10 3 ksh
0.0 0.0 0 0 mega Jun10 7 ksh

Any help is appreciated

Thank you

konsolebox 08-06-2013 10:12 AM

Try grep -e ' -\?ksh$' topCPU.log.

megatron521 08-06-2013 10:18 AM

well, no luck

konsolebox 08-06-2013 10:34 AM

What do you mean, no output? You copy/pasted it when executing?

megatron521 08-06-2013 10:43 AM

yeah, there is no output

Code:

:
=> grep -e '-\?ksh$' topCPU.log
:

=> grep ksh topCPU.log
 3.9  0.0  10004    1436      user        Jun25  26900 /bin/ksh /apps/sa/tools/monitor.ksh
 0.0  0.0      0      0      user        Jun10      7 ksh
 0.0  0.0      0      0      root        Jun10      16 -ksh
 0.0  0.0      0      0      root        Jun10      18 ksh
 1.8  0.0    9884    1452      user        01:15    3375 /bin/ksh /apps/sa/tools/abc.ksh
 0.0  0.0      0      0      root        Jun10      17 -ksh
 1.9  0.0    9876    1440      user        03:00  18645 /bin/ksh /apps/sa/adhoc/ch
 3.8  0.0    9884    1452      user        05:15  14884 /bin/ksh /apps/sa/tools/lo
:
=>


konsolebox 08-06-2013 10:49 AM

Code:

'-\?ksh$' topCPU.log
There's supposed to be a space between ' and -. You didn't copy it well.
Try this one too if it doesn't work as the shell might parse it differently.
Code:

grep -e " -\\?ksh$" topCPU.log

megatron521 08-06-2013 10:57 AM

Hello - I tried with and without space earlier, as mentioned it was not able to find the entries in the log file

Tried with double quotes too, no output :-(

konsolebox 08-06-2013 11:37 AM

Try these variations as well:
Code:

grep -e " -?ksh$" topCPU.log
grep -e " -\\?ksh$" topCPU.log
grep -e "[[:blank:]]-?ksh[[:blank:]]*$" topCPU.log
grep -e "[[:blank:]]-\\?ksh[[:blank:]]*$" topCPU.log
grep -e "[[:space:]]-?ksh[[:space:]]*$" topCPU.log
grep -e "[[:space:]]-\\?ksh[[:space:]]*$" topCPU.log
grep -e " -?ksh *$" topCPU.log
grep -e " -\\?ksh *$" topCPU.log

If nothing still works, please give us the output of
Code:

echo grep -e " -\\?ksh *$" topCPU.log

megatron521 08-06-2013 12:23 PM

Thanks alot, it worked
All the 3 below commands were giving expected output
Code:

grep -e "[[:blank:]]-\\?ksh[[:blank:]]*$" topCPU.log
grep -e "[[:space:]]-\\?ksh[[:space:]]*$" topCPU.log
grep -e " -\\?ksh *$" topCPU.log

output:
 0.0  0.0      0      0      root        Jun10      7 ksh
 0.0  0.0      0      0      root        Jun10      16 -ksh
 0.0  0.0      0      0      root        Jun10      18 ksh
 0.0  0.0      0      0      root        Jun10      17 -ksh

Goodday


All times are GMT -5. The time now is 11:29 AM.