Lately, I have been experimenting with the ps command, and sometimes long paths wrap to the next line (or two) and make it hard to read. I want to pipe the ps output into another program to limit the output to x number of characters.

This work for me. Can you provide some output?
–
cuonglmApr 18 '14 at 4:29

where is the dynamic part? You want a different length for certain conditions? It seems like you want to limit all output to one maximum length
–
user61786Apr 18 '14 at 4:29

@awk_FTW Depending on the situation, I have different window settings. At fullscreen, my width is 167. But at other sizes, I will need to be smaller. That's why I want to call ssty to get the size.
–
lentilsApr 18 '14 at 4:34

3 Answers
3

The problem seems to be that stty needs to have the tty on its standard input in order to function. The above two approaches solve that.

There is still another option. While stty's stdin and stdout are both redirected in the above commands, its stderr is not: it still points to a terminal. Strangely enough, stty will also work if it is given stderr as its input:

Indeed, it does! This was exactly what I was looking for. Just curious, why did you add the tee tt at the end?
–
lentilsApr 18 '14 at 4:39

@lentils Oops, the tee was added when I was debugging. It is gone now.
–
John1024Apr 18 '14 at 4:41

What does <&2 refer to? I have never seen that notation before.
–
lentilsApr 18 '14 at 5:00

@lentils In the shell, files are numbered. Zero refers to standard input, one to standard output, and two to standard error. The expression <&2 means get your input from standard error.
–
John1024Apr 18 '14 at 5:21

1

stty gets the settings of the terminal opened on its stdin (you do stty < /dev/other-tty to get the settings of another terminal for instance). So it's not strange that it won't work if its stdin is a pipe or that <&2 works.
–
Stéphane ChazelasApr 18 '14 at 5:58

Some shells like zsh, bash or mksh automatically set the $COLUMNS variable to the width of the terminal, so you don't need to invoke stty here.

All the implementations of ps I tried that support that non-standard (BSD-type) syntax query the terminal width by themselves. I'm surprised yours doesn't. I expect it will look at the content of the COLUMNSenvironment variable though.

That will tell ps to parse its output to your specifications as necessary.

Of course, if you don't word wrap, though, then you've got the problem of missing info. Do you really need all of it for all processes? Open it in a pager if so:

ps ww ${opts} | $PAGER

If not, specify what you do want to see:

ps -o args= -p $pid

Alternatively you can explicitly inform ps of your terminal --width:

man ps
...

w Wide output. Use this option twice for unlimited width.

-w Wide output. Use this option twice for unlimited width.

--width n Set screen width.

The --width thing works exactly to your requested specs without having to involve any additional filters or ancillary processes (which will likely only clutter your -aux output even more). And with $COLUMNS as shown above and as Stephane points out it will even work dynamically.

It's probably worth noting, though, that I find people often try to add these kinds of unnecessary filters so they can accomodate a |pipe through another filter to parse output which is also pretty likely to be unnecessary. Of course, by people I mostly mean me.

The -output operand I mention above allows you to filter which columns ps displays, and when you add the =assigment you can even name the column as you please. I leave the assignment empty and hand it a target -process $pid so the only output from ps at all is the $pid command name and its args at invocation. And -o barely scratches the surface of how you can define what ps will or won't display. This is the direction I would recommend you take, especially via:

man ps
...

To see every process on the system using BSD syntax:

ps ax

ps axu

To print a process tree:

ps -ejH

ps axjf

To get info about threads:

ps -eLf

ps axms

To get security info:

ps -eo euser,ruser,suser,fuser,f,comm,label

ps axZ

ps -eM

To see every process running as root (real & effective ID) in user format: