Command line...

Could someone please explain that this command line does in English please?


 ps gu|head -n 1;ps gau|egrep -v "CPU|kproc"|grep "${user}"|grep
-v grep|sort +2b -3 -n -r|head -n 10;uptime

everything supposed to be on one(1) line.

i've got different results when running it on Unix and Linux, not sure I understand why.

Thank you much.

Take it command by command: - Looks like you have two seperate command sequences here. The first being the ps -gu | head -n 1 and the second starting from the ps -gau ...

first set
ps -gu -- gives a display of all processes by group name in a user readble format
head -n 1 --prints the the first line of output piped to it from the first command.
The output of this command is probably (I didnt
run it) just the headings of the ps command.

second set
ps -gau - selection of all processes, in a readable format
egrep -v "CPU|kproc"|grep "${user}"|grep
-v grep|- a search string, basically it ignores and does not output
any lines that have "CPU" or "kproc" in them and outputs all lines
with the value of ${user}. It also ignores any grep string.

sort +2b -3 -n -r|head -n 10 - numeric sort of teh 2nd and third
fields, ignoring blank space and in reverse order.
uptime - prints a display of how long the system has been running

It appears this is just a command set to get all processes for a given user, in a sorted format.

google, thank you for your translation. However, I'm not sure why when I put in 'root' as $user, no processes started by root came up.
:confused:

I don't understand how that can work.

ps -gu is an illegal option on any server I have seen. If you specify -g or -u, you must suppy a list of groups or users.

Below is the usage doc from ps on our sun system.

usage: ps [ -aAdeflcjLPy ] [ -o format ] [ -t termlist ]
[ -u userlist ] [ -U userlist ] [ -G grouplist ]
[ -p proclist ] [ -g pgrplist ] [ -s sidlist ]

What is the correct string. While I know that this is outside the original scope of the question, I am curious (yellow).

Not sure i understand your question of "what is the correct string".

I'm basically trying to display cpu usage of each process with the option of displaying for a specified user or for all users.

There are many versions of unix and I can believe that this command made sense on one of them.

Look at the internal piece:
ps gau|egrep -v "CPU|kproc"|grep "${user}"|grep -v grep|sort +2b -3 -n -r|head -n 10

First it runs "ps gau".
Then those "grep" commands throw some stuff away.
Then what's left gets sorted.
Then we keep the top 10 lines.

So run "ps gau". No matter what you put in $user, you can only pick and choose from the lines output by the ps command.

On Linux, the g is legal and says to keep process group leaders. And yes the command is legal without a minus sign. On SunOS, I get an error message. It kinda works on HP-UX, but only because "ps xyzzy" works too.

I totally forgot about linux. Thanks for the reply

Thank you guys! you're all best!
:smiley:

i'm trying to make this script to work on both Linux and SunOS, is there a different command that i could use in place of ps that would do the same thing? Like you said SunOS won't allow ps gu... :frowning:

The command "ps -ef" says to return extended information information on ALL processes. This sould work on any flavor of unix

Thanks for your reply, but "ps -ef" does not give me %CPU, Start time, State....etc or the process.

Try this one on your system.

ps -e -o user,pid,ppid,pcpu,stime,etime,time,comm

when i pipe this command to grep for a $user, the header get cut off. how do i fix that?

ps -e -opid= -opcpu= -oc= -oargs=|grep "${user}"

This command does the same thing, the head gets cut off. without the grep for user then the head displays... :confused:

Try

ps -e -o pid,pcpu,comm|head -n1;ps -e -o pid,pcpu,comm|tail +2|grep "${user}"

Tried that and this is what i got:

$ ps -e -o pid,pcpu,comm|head -n1;ps -e -o pid,pcpu,comm|tail +2|grep "${user}"
  PID %CPU COMMAND
$ 

running on Linux.

Processes current user

ps -u $user -o user,pid,pcpu,comm

Processes all users

ps -e -o user,pid,pcpu,comm

Processes specific users

ps -u root,nobody -o user,pid,pcpu,comm

Sorry for the run around. I had forgotten about the "-u" option.