Sponsored Content
Full Discussion: grep hogs entire cpu
Special Forums UNIX and Linux Applications grep hogs entire cpu Post 302590828 by admin_xor on Tuesday 17th of January 2012 05:18:39 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
chroot(2)							   System Calls 							 chroot(2)

NAME
chroot, fchroot - change root directory SYNOPSIS
#include <unistd.h> int chroot(const char *path); int fchroot(int fildes); DESCRIPTION
The chroot() and fchroot() functions cause a directory to become the root directory, the starting point for path searches for path names beginning with / (slash). The user's working directory is unaffected by the chroot() and fchroot() functions. The path argument points to a path name naming a directory. The fildes argument to fchroot() is the open file descriptor of the directory which is to become the root. The privilege {PRIV_PROC_CHROOT} must be asserted in the effective set of the process to change the root directory. While it is always pos- sible to change to the system root using the fchroot() function, it is not guaranteed to succeed in any other case, even if fildes is valid in all respects. The ".." entry in the root directory is interpreted to mean the root directory itself. Therefore, ".." cannot be used to access files out- side the subtree rooted at the root directory. Instead, fchroot() can be used to reset the root to a directory that was opened before the root directory was changed. RETURN VALUES
Upon successful completion, 0 is returned. Otherwise, -1 is returned, the root directory remains unchanged, and errno is set to indicate the error. ERRORS
The chroot() function will fail if: EACCES Search permission is denied for a component of the path prefix of dirname, or search permission is denied for the directory referred to by dirname. EBADF The descriptor is not valid. EFAULT The path argument points to an illegal address. EINVAL The fchroot() function attempted to change to a directory the is not the system root and external circumstances do not allow this. EINTR A signal was caught during the execution of the chroot() function. EIO An I/O error occurred while reading from or writing to the file system. ELOOP Too many symbolic links were encountered in translating path. ENAMETOOLONG The length of the path argument exceeds PATH_MAX, or the length of a path component exceeds NAME_MAX while _POSIX_NO_TRUNC is in effect. ENOENT The named directory does not exist or is a null pathname. ENOLINK The path argument points to a remote machine and the link to that machine is no longer active. ENOTDIR Any component of the path name is not a directory. EPERM The {PRIV_PROC_CHROOT} privilege is not asserted in the effective set of the calling process. SEE ALSO
chroot(1M), chdir(2), privileges(5) WARNINGS
The only use of fchroot() that is appropriate is to change back to the system root. SunOS 5.10 20 Jan 2003 chroot(2)
All times are GMT -4. The time now is 07:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy