8

I like this pattern ps aux | grep something.

This way I can find the needed information easily without remembering command line options for the ps command.

Unfortunately the ps commands cuts the linux user name (first column) after 7 characters and adds a + if the username is longer.

In my case it is important since the usernames are like "foobar_123" and "foobar_234".

I know that I could use the following command, but it would be very nice if I could still use the ps aux | grep something pattern.

ps ax o user:16,pid,pcpu,pmem,vsz,rss,stat,start_time,time,cmd

How to get above format via configuration, so that ps aux | grep something does not cut the username?

Hint: Answers like "use ps ... special...args..." are don't match above question.

Version: procps-ng version 3.3.5

7

3 Answers 3

7
+50

If you know that a long command with multiple options does what you want, but you don't want to type it each time, then (assuming you're using Bash) you can create an alias for that command to make it easier. For example:

alias ps_mod='ps ax o user:16,pid,pcpu,pmem,vsz,rss,stat,start_time,time,cmd'

Then you can just type that simple command. You can add this line to your ~/.bash_profile (or ~/.bashrc, depending on your system) file, so that it is defined automatically on login.

If you're not using Bash, then you can probably do something similar by defining a shell function instead.

1
  • 2
    You could even set the alias name to 'ps', which would effectively 'modify' the ps command for your system.
    – Time4Tea
    Nov 10, 2017 at 14:05
4

Tiny patch to procps-ng source

Seems a quick patch of the source code would do the trick; normally the user column is set to 8 and will truncate as detailed below.

Patch applies to commit 64fa8898 which was tagged v3.3.5 (linked above)

You might as well download the latest source if you're going to compile it though—I'd suggest downloading the latest source and edit the "USER" line in procps-ng source file ps/output.c from 8 to 16 as below instead of patching the old (2013-ish) version:

---
 ps/output.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ps/output.c b/ps/output.c
index 9644ed3..41c2eda 100644
--- a/ps/output.c
+++ b/ps/output.c
@@ -1564,7 +1564,7 @@ static const format_struct format_array[] = {
 {"uname",     "USER",    pr_euser,    sr_euser,   8, USR,    DEC, ET|USER}, /* man page misspelling of user? */
 {"upr",       "UPR",     pr_nop,      sr_nop,     3,   0,    BSD, TO|RIGHT}, /*usrpri*/
 {"uprocp",    "UPROCP",  pr_nop,      sr_nop,     8,   0,    BSD, AN|RIGHT},
-{"user",      "USER",    pr_euser,    sr_euser,   8, USR,    U98, ET|USER}, /* BSD n forces this to UID */
+{"user",      "USER",    pr_euser,    sr_euser,  16, USR,    U98, ET|USER}, /* BSD n forces this to UID */
 {"usertime",  "USER",    pr_nop,      sr_nop,     4,   0,    DEC, ET|RIGHT},
 {"usrpri",    "UPR",     pr_nop,      sr_nop,     3,   0,    DEC, TO|RIGHT}, /*upr*/
 {"util",      "C",       pr_c,        sr_pcpu,    2,   0,    SGI, ET|RIGHT}, // not sure about "C"
-- 

Addendum

According to the source for the ps:

// The Open Group Base Specifications Issue 6 (IEEE Std 1003.1, 2004 Edition)
// requires that user and group names print as decimal numbers if there is
// not enough room in the column.  However, we will now truncate such names
// and provide a visual hint of such truncation.  Hopefully, this will reduce
// the volume of bug reports regarding that former 'feature'.
//
// The UNIX and POSIX way to change column width is to rename it:
//      ps -o pid,user=CumbersomeUserNames -o comm
// The easy way is to directly specify the desired width:
//      ps -o pid,user:19,comm
//
2
  • Wow, yes, this would be a solution. Do you think the upstream developers will integrate this patch?
    – guettli
    Nov 15, 2017 at 15:54
  • I think it's doubtful as width may be specified in a standard somewhere—it's a trivial patch and sort of has a command line option in that you can specify as in addendum above. Nov 15, 2017 at 18:05
1

This question was posed in 2014 on the askubuntu.com forums

My solution is inspired by the above link, but made with some portable goodness. Add this function to your .bashrc and get on with it!

ps() {
    if [[ $@ =~ .u* || *u ]]; then
        command getent passwd |\
        awk -F':' ' \
        !len || length($1) > len {len=length($1);s=$1}\
        END{print s, len; system("ps axo user:"len",pid,pcpu,pmem,vsz,rss,tty,stat,start,time,comm");}'
    else
        command ps "$@"
    fi
}

Add the above function to your ~/.bashrc, ps aux | grep someshit as you usually would, and enjoy output with your columns formatted for the largest possible username as determined in a friendly regular user way from /etc/passwd using getent -- then parsed via awk with the help of bash for saving the integer value related to the longest username string.

Now when I

$ ps axu | grep this

or

$ ps uaz | grep this

or

$ ps aux | grep this

Output:

thisisareallylonguser 9289 0.0 0.0 23192 4716 pts/6 S 17:59:54 00:00:00 bash

Let me know if you want me to step through the bash function in the event that it isn't immediately clear.

2
  • Looks like black magic to my eyes :-)
    – guettli
    Nov 16, 2017 at 13:25
  • 1
    fyi - our userid's mapped to a WinAD, and usernames were in the form DOMAIN\user. The `` escaped the following Char, decrementing the Length by 2. I path of least resistance'd it and hardcoded a length. (22) in the line: ##!len || length($1) > len {len=length($1);s=$1}\ !len || length($1) > len {len=22;s=$1}\ as a workaround - if I swing back to add the logic, I'll put it up here. This is a GREAT Solution though!! Thank you!
    – jabutler
    Jul 10, 2020 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .