grep hogs entire cpu

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications grep hogs entire cpu
# 1  
Old 01-11-2012
grep hogs entire cpu

How can grep hog your entire cpu? I am absolutely shocked by this.
# 2  
Old 01-11-2012
Entire CPU ??!! Are you logging CPU based on numbers or by usage ..

In Solaris,

If by number wise - mpstat command will help you ..

If by usage wise - prstat ..
# 3  
Old 01-11-2012
Quote:
Originally Posted by jayan_jay
Entire CPU ??!! Are you logging CPU based on numbers or by usage ..

In Solaris,

If by number wise - mpstat command will help you ..

If by usage wise - prstat ..
All I was doing is this.

Code:
sudo grep -ri "xprofile" /etc/* 2>/dev/null

My gui completely froze so I hit ctrl+alt+f2 to open up a new console. Then I used used top to check out what was going on. top said I was at 100% cpu usage and grep was hogging my entire cpu.
# 4  
Old 01-17-2012
Hmm.
Quote:
sudo grep -ri "xprofile" /etc/* 2>/dev/null
This will search every inode in directory /etc whether or not that inode is suitable for "grep". You can even find pipes under /etc .
On my system it would try to search over 8000 inodes.

As a minimum, consider limiting the list by using "find -xdev -type f" and then further checking for text files with the "file" command.
# 5  
Old 01-17-2012
Is there any particular reason you were running it as root? Nearly nothing in /etc/ needs root to be read. Using root needlessly is dangerous.

Quote:
Originally Posted by COKEDUDE
All I was doing is this.

Code:
sudo grep -ri "xprofile" /etc/* 2>/dev/null

My gui completely froze
grep has no power to freeze your GUI. It only gets as much CPU time as it's given, it can't hog it. This was an OS or GUI crash of some sort.

Is this the same system you were having severe system instability with before? Did you ever really track that down?
# 6  
Old 01-17-2012
You better use find as suggested. When I forget (that happens more often) about the location of any config file or any such thing, but somehow if I remember any unique word in that file I do this:
Code:
find /etc -xdev -type f | xargs grep -i "unique_word"

This is lot faster.

@Corona688
Some directories in /etc are indeed restricted for root. This depends on what UNIX/Linux variant you are using and the installed software/packages. See below:
Code:
##for a RHEL 6.1 
$ ls -ltr /etc/* 2>err
$ cat err
ls: cannot open directory /etc/sudoers.d: Permission denied
ls: cannot open directory /etc/dhcp: Permission denied
ls: cannot open directory /etc/named: Permission denied
ls: cannot open directory /etc/sssd: Permission denied
ls: cannot open directory /etc/audisp: Permission denied
ls: cannot open directory /etc/audit: Permission denied

##for a Solaris11
$ ls -ltr /etc/* 2>err
$ cat err
/etc/sudoers.d: Permission denied

##for a FreeBSD
$ ls -ltr /etc/* 2>err
$ cat err
ls: /etc/ntp: Permission denied

What if the file you are searching are in one of those directories.

Also, my RHEL 6.1 VM took 100% of CPU time of 4 allocated cores (with only 953 inodes in /etc directory):
Code:
top - 23:24:54 up 18 min,  2 users,  load average: 0.20, 0.05, 0.02
Tasks: 173 total,   2 running, 171 sleeping,   0 stopped,   0 zombie
Cpu(s): 25.0%us,  0.2%sy,  0.0%ni, 74.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    756832k total,   380896k used,   375936k free,     5760k buffers
Swap:  1540088k total,        0k used,  1540088k free,   219216k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 2522 root      20   0  101m 1468  724 R 100.0  0.2   0:08.52 grep
 2055 unixuser  20   0 15088 1284  952 R  0.3  0.2   0:01.35 top
    1 root      20   0 19396 1544 1248 S  0.0  0.2   0:00.96 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.01 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.09 migration/0
    4 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
    6 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    7 root      RT   0     0    0    0 S  0.0  0.0   0:00.09 migration/1
    8 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/1
    9 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/1
   10 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/1
   11 root      RT   0     0    0    0 S  0.0  0.0   0:00.08 migration/2
   12 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/2
   13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/2
   14 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/2
   15 root      RT   0     0    0    0 S  0.0  0.0   0:00.10 migration/3
   16 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/3

So there's a pretty good reason why your GUI got stuck as grep has to run a loop for each inode (that includes directories as well which is absolutely meaning less in this case).

Use find!!!Smilie
# 7  
Old 01-17-2012
Quote:
Originally Posted by admin_xor
@Corona688
Some directories in /etc are indeed restricted for root.
So? He's not looking inside any directories.

Quote:
So there's a pretty good reason why your GUI got stuck as grep has to run a loop for each inode (that includes directories as well which is absolutely meaning less in this case).
So what? That doesn't hang the system. Linux is a pre-emptive kernel. If a process wastes too much time, it will be stopped so something else can go. At worst grep could've lagged the system from using 100% CPU. It wouldn't freeze it.

Back in my college days, a fellow student accidentally left behind a process which due to a bug did nothing but eat 100% CPU. Several weeks later, it was still there. Nobody noticed it until I ran 'top'. It hadn't interfered with people's use of the system at all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional grep command to search entire file

Let me give you a complete example what I am trying to achieve. 1. Below is the log file structure where I need 2,5 and 14th column of the logs after grepping through the linkId=1ddoic. Log file structure:- abc.com 20120829001415 127.0.0.1 app none11111 sas 0 0 N clk Mozilla/5.0... (3 Replies)
Discussion started by: kmajumder
3 Replies

2. UNIX for Advanced & Expert Users

grep for entire line from a log folder

As per my understanding below mentioned line of code finding a word 'boy' in $ACULOG... num_errors=`grep -i -e fail -e illegal -e exception -e "<E" -e boy $ACULOG | wc -l` if I'm not corerct, please correct me. How I can find entire line like "This is a boy" with something similar as above... (1 Reply)
Discussion started by: heyitsmeok
1 Replies

3. Shell Programming and Scripting

grep to find entire line ....

As per my understanding below mentioned line of code finding a word 'boy' in $ACULOG... num_errors=`grep -i -e fail -e illegal -e exception -e "<E" -e boy $ACULOG | wc -l` if I'm not corerct, please correct me. How I can find entire line like "This is a boy" with something similar as above... (1 Reply)
Discussion started by: heyitsmeok
1 Replies

4. AIX

Need a list of top 10 CPU using processes (also top 10 memory hogs, separately)

Okay, I am trying to come up with a multi-platform script to report top ten CPU and memory hog processes, which will be run by our enterprise monitoring application as an auto-action item when the CPU and Memory utilization gets reported as higher than a certain threshold I use top on other... (5 Replies)
Discussion started by: thenomad
5 Replies

5. Shell Programming and Scripting

Grep for a pattern and print entire record

Hi friends, This is my very first post on forum, so kindly excuse if my doubts are found too silly. I am trying to automate a piece of routine work and this is where I am stuck at the moment-I need to grep a particular ID through a file containing many records(which start with <LRECORD> and end... (6 Replies)
Discussion started by: faiz1985
6 Replies

6. UNIX for Dummies Questions & Answers

grep question: stop search from finding entire line

Sorry for the title, I really don't know how to word this question or what to even search for. I tried "grep one match", "grep 1 match", "stop grep" on both google and here and haven't found something that helps, so here I go: I have a file that's about 1.5 million lines long, every line looks... (3 Replies)
Discussion started by: rmoakler
3 Replies

7. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

8. Linux

grep -f CPU performances

Hi I would like to thank you all for this excellent forum. Today i tried to compare two files and i get some problem with it. I have two files and i want to get all the data that match the first file like this File1 (pattern file) ___________________________ 9007 9126 9918 9127 ... (6 Replies)
Discussion started by: tafil
6 Replies

9. UNIX for Dummies Questions & Answers

grep entire statement not just line

(extract from SQL binlog file...) # at 4960 #080801 14:35:31 server id 4 end_log_pos 195 Query thread_id=63121426 exec_time=0 error_code=0 use d_jds; SET TIMESTAMP=1217581531; UPDATE bid_details set bidding = 3170.37 ,deduction=if((3170.37 < 37.43),0,deduction) where... (3 Replies)
Discussion started by: shantanuo
3 Replies

10. UNIX for Dummies Questions & Answers

grep Vs CPU usage

Hi, I have one basic doubt, that using grep command frequently , will it have direct impact on the CPU load, pls clarify for eg, if i run a non stop script containing while loop to grep some parameters, what will be the load in CPU.. thanks (3 Replies)
Discussion started by: vasikaran
3 Replies
Login or Register to Ask a Question