3

Is there a way to execute ps ax | grep java without it wrapping on the terminal?

2
  • which OS? Which terminal app?
    – keithosu
    Sep 2, 2009 at 18:32
  • OS X Terminal specifically, but I've bumped into this on other platforms. Sep 2, 2009 at 19:15

3 Answers 3

4

For me, ps doesn't wrap unless I do:

ps axw

However, you can set the screen width like this to truncate the output (but it won't override -w):

ps ax --width=80

You can also use the o (or -o or --format) option to include only columns that you are interested in, change or eliminate column headers and set the width of each column individually*. See man ps and search for "user-defined format" (multiple occurrences).

* setting a column width smaller than normal may change the way the contents or displayed or may not have an affect. For example, "args:20" doesn't truncate the output (unless it's not the last column) and "user:5" causes usernames longer than five characters to be displayed as the UID number.

0
1

I am not able to comment on Dennis Williamson's answer (maybe I have too few points for that); that's why I am writing a new answer.

You can do

ps ax --width=$COLUMNS

to get the output width adjusted according to the current terminal size. If your favorite shell does not support COLUMNS environmental variable, you can use stty:

ps ax --width=$(stty -a | grep 'columns [0-9]*;' | sed 's|.*columns \([0-9]*\).*|\1|')

or more specifically (using single awk but making stronger assumption on the stty -a output format):

ps -axw --width=$(stty -a | awk '/columns/ { printf "%d", $7 }')

All this is quite a bit of typing, so you might want to consider making the above an alias or a shell function... :-) The above maybe needs to be adjusted for your operating system (I cannot test now, I am not using OS X but Linux). I have the following output for stty -a:

speed 38400 baud; rows 42; columns 178; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
flush = ^O; min = 1; time = 0;
...
0

Appending less -S at the end of any command would unwrap the command output on Linux terminal.

Example: 1 (Your original command appended with less -S)

ps ax | grep java | less -S

16338 ?        Sl   725:52 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.moduleName=/opt/mqm/mq_mount
23137 ?        Sl   498:18 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.moduleName=/opt/mqm/mq_mount
34753 ?        Sl   492:43 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.moduleName=/opt/mqm/mq_mount
39519 ?        Sl   486:47 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.moduleName=/opt/mqm/mq_mount
42071 ?        Sl   494:36 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.moduleName=/opt/mqm/mq_mount

Example: 2. Even better, next command reveals % CPU and Memory consumption of each Java thread/Process ID.

ps aux | egrep 'java|USER' | less -S

USER         PID  %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
Kathpalia   14876  0.0  0.0 103328   864 pts/0    S+   09:31   0:00 egrep java|USER
Kathpalia   16338  2.7  9.4 5932240 953984 ?      Sl   Mar22 726:00 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.
Kathpalia   23137  2.3  5.3 5875976 542084 ?      Sl   Mar25 498:26 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.
Kathpalia   34753  2.3  4.6 5837004 469692 ?      Sl   Mar25 492:51 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.
Kathpalia   39519  2.3  4.2 5824768 429004 ?      Sl   Mar25 486:55 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.
Kathpalia   42071  2.3  4.0 5825876 408612 ?      Sl   Mar25 494:44 /usr/java/jdk1.8.0_60/bin/java -Dinstall4j.jvmDir=/usr/java/jdk1.8.0_60 -Dexe4j.

It's true for any generic process:

ps aux | egrep 'MyGenericProcess|USER' | less -S

PS: For Linux 6.x or higher, grep -E can be used instead of egrep

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.