Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

See complete output of ps command UNIX
I don't know if this is common knowledge, but here goes ... when using grep on the output of a ps -aux (or similar) command rarely found the line that I was looking for. For example, this would fail:
$ ps -aux|grep stringToSearchFor
Obviously, stringToSearchFor is actually the program I was looking for. I even tried redirecting the ps output to a text file and and then used the cat command in a similar fashion:
$ ps -aux>file.txt
$ cat file.txt|grep stringToSearchFor
Looking at file.txt with TextEdit showed that the output of ps is only as wide as there are columns in the current terminal. Resizing the Terminal only goes so far, and trying to change the COLUMNS environment variable doesn't help either. Thus if the string you are looking for would occur off the terminal screen when ps runs, it doesn't exist. So it is very difficult to find!

According to the man file for ps, using the w option forces the output to use 132 columns, which helps little. But if you specify it twice, it forces ps to use as many columns as necessary. Thus long lines are displayed wrapping to the the next line. So if you are looking for lines that you think may be longer than the Terminal's width, use:
$ ps -auxww | grep stringToSearchFor
So using grep as before will now work, as ps will display all of the text for every listed job.

[robg adds: We've shown ps examples before that use the ww setting, but never actually explained it. For you Unix wizards out there, I realize this is a very basic tip. But for those who are still learning their way, it's a useful bit of knowledge...]
    •    
  • Currently 2.71 / 5
  You rated: 3 / 5 (7 votes cast)
 
[16,329 views]  

See complete output of ps command | 14 comments | Create New Account
Click here to return to the 'See complete output of ps command' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
useless use of cat!
Authored by: merlyn on Jul 26, '05 10:00:59AM
You said:

cat file.txt|grep stringToSearchFor
This is a "useless use of cat". "cat" is for concatenating. You're not concatenating. You could have, should have, might have, written:

grep stringToSearchFor file.txt


[ Reply to This | # ]
useless use of cat!
Authored by: bendybendy on Jul 26, '05 10:49:12AM

I use cat like this all the time. You can try different search strings at the end of the command, using the up-arrow to recall the last bash command. Doesn't make sense for one search, but if you are going to try a bunch of differnet search strings, it's quick.



[ Reply to This | # ]
useless use of cat!
Authored by: googoo on Jul 26, '05 12:28:15PM
Merlyn's point is that you do not need to send the file(s) through cat in order to grep them. Just leave out the cat part and grep the files with

$ grep search_string file_name(s)

It even works with wildcards and other shell patterns. Try it (and check out man grep).

-Mark

[ Reply to This | # ]

useless use of cat!
Authored by: lullabud on Jul 26, '05 02:21:57PM
Both ways do the same thing and of course omitting a "useless" `cat` would be more efficient, but assuming you will be repeating the command several times with different search strings, it's much easier to arrow back from the end of the line only a few characters, rather than all the way through the filename, especially if the filename is a hundred or so characters long when the path is included. eg:

cat some_long_file_name.txt | grep 'somethingtosearch'
cat some_long_file_name.txt | grep 'somethingtosearchfor'
In that example, the second line only requires pressing the up arrow key once, the left arrow key once, then typing the string 'for'. The other way, `grep 'somethingtosearch' some_long_file_name.txt` would require arrowing all the way back through the file name, or else doing ctrl-a and arrowing all the way forward through the search string. So, really, the usage of `cat` in the original example does have a good use under certain circumstances.

[ Reply to This | # ]
useless use of cat!
Authored by: kps on Jul 26, '05 07:01:54PM

<some_long_file_name.txt grep 'somethingtosearch'
<some_long_file_name.txt grep 'somethingtosearchfor'


[ Reply to This | # ]
better use of history
Authored by: muddmatt on Jul 26, '05 03:12:54PM
if you are going to try a bunch of differnet search strings, it's quick

A better idiom:

% grep foo /path/to/file.txt
% grep bar !$
"!$" means "last argument of the previous command". See the bash manpage for other (highly useful) history tokens.

[ Reply to This | # ]
better use of history
Authored by: rootpoot on Jul 26, '05 09:13:35PM
You can also use
<esc> .
to access the last argument.

[ Reply to This | # ]
See complete output of ps command
Authored by: bindu on Jul 26, '05 10:37:03AM
handy tip along this simple hint, for those who are not familiar with unix...

not sure of the case of the string 'stringToSearchFor'? then use:

grep -i stringToSearchFor
that will even match strINgtoSEARCHfor.

(if you are a unix wizard, pardon my post, but i have found the ignore-case flag so useful (and it allows one to be a little lazy too:))



[ Reply to This | # ]
See complete output of ps command
Authored by: TrumpetPower! on Jul 26, '05 11:52:57AM

When doing case-insensitive searches, it’s a good idea to specify the search string in all lowercase. In some programs, if you specify a case-insensitive search but use uppercase characters in the search string, it'll assume you really know what you're doing and want a case-sensitive search anyway.

Without checking the man page for OS X’s grep, I couldn’t tell you which it is…but it’s a good habit to get into nonetheless.

Cheers,

b&



[ Reply to This | # ]
See complete output of ps command
Authored by: Thom on Jul 26, '05 10:37:18AM

Consider piping the output to 'less', where you can hit spacebar to go a page forward, 'b' to go a page back (and q to quit when you're done); also '/' (forward slash) lets you search amongst the text you're looking through.

Also, when you grep, you may want to consider using the -i flag, so you don't have to worry about things being case-sensitive.



[ Reply to This | # ]
See complete output of ps command
Authored by: ChaChi on Jul 26, '05 12:40:19PM
If you're only searching for a process name then you can use ps with "-c" as well to limit ps's output to just the names of the processes and not the full path:
ps -acux | grep -i processname


[ Reply to This | # ]
See complete output of ps command
Authored by: geordie on Jul 26, '05 02:57:00PM
I just put the following in my .bashrc file and when I need to find a specific process I type psgrep processname.

psgrep() { ps -acux | grep -i $1 | grep -v grep }

You will need to type source ~/.bashrc to make it take affect immediately for the open window.

[ Reply to This | # ]

See complete output of ps command
Authored by: sweth on Jul 26, '05 03:32:17PM

As long as we're rehashing old unix tricks, and in the same vein as the "useless use of cat" comments, the second grep in your psgrep isn't necessary, and under some circumstances can actually hide relevant results.

The "proper" way to do this is to make sure that the regex that you are looking for does not actually match itself. The easiest way to do this is to replace one character from the regex with a character class that only matches that character; for example, the regex "[v]i" will match the string "vi", but will not match the string "[v]i", so a "ps auwwx | grep '[v]i'" won't include either of its subcommands in its results.

To automate this for a function, you can do something like

function psgrep {
ENDING="${1#?}"
ps auwwx | grep -i "[${1%${ENDING}}]${ENDING}"
}

.



[ Reply to This | # ]
See complete output of ps command
Authored by: kholburn on Jul 27, '05 11:01:52AM
Here is a bash function I use for ps. I find this has information I usually need.


$ type myps
myps is a function
myps () 
{ 
    if [ $# -gt 0 ]; then
        ps -awwwxo "pid ppid %cpu %mem user command" | egrep "PID|$1" | grep -v grep;
    else
        ps -awwwxo "pid ppid %cpu %mem user command" | grep -v grep;
    fi
}
also this for when I need to see the environments:

$ type pse
pse is a function
pse () 
{ 
    if [ $# -gt 0 ]; then
        ps -awwwexo "pid ppid %cpu %mem user command" | egrep "PID|$1" | grep -v grep;
    else
        ps -awwwexo "pid ppid %cpu %mem user command" | grep -v grep;
    fi
}



[ Reply to This | # ]