Displaying current user process

When I typed in ps -a I get this:

PID TTY TIME CMD
31799 pts/3 00:00:00 vim
31866 pts/3 00:00:00 vim

And to check who is currently logged in, I type who
Felix Whoals
Tada Whoals
Lala Whoals

How can I get the user process for all current users who logged in?? I think I need to combine both ps -a and who but don't know which command to use.

The result has to be something like this:

Felix Whoals
PID TTY TIME CMD
31799 pts/3 00:00:00 vim
31866 pts/3 00:00:00 vim

Tada Whoals
PID TTY TIME CMD
8368 pts/0 00:00:00 vim

Lala Whoals
PID TTY TIME CMD
31799 pts/3 00:00:00 vim
31866 pts/3 00:00:00 vim

I just want to know the commands to be used. I think this is pretty easy but I just couldn't figure it out the solution.

I would use "who" to see who is logged in.

Then build a list of usernames and ensure it has no duplicates.

Then use "ps -u <username>" to get their processes.

Not sure what OS you're running and this might be a tad over kill, but it may help you figure out your own solution:

for USER in `who | awk '{print $1}' | uniq`;do echo "PROCESSES RUN BY $USER";ps -a -u $USER;done

This one-line for loop loops through the names listed by the who command. Awk grabs the first column containing the names ($1), and uniq makes sure any duplicates are omitted. Then a header (PROCESSES RUN BY) followed by the user's name is printed. Under this heading are the processes being run by that particular user.

Hope this helps.

Hi there,

I have a question, when I do ps -au xxx, I get an answer for user xxx.. I have a filename call abc and when I do head -1 abc i get an output of xxx too. Why can't I do ps -au head -1 abc?

You need to follow the syntax for ps . The -u means user. It doesn't accept commands along with it.

Well because when I do head -1 abc it gives me a username. therefore when I do ps -au head -1 abc, I should get an answer shouldn't I?

May I know how can I solve this problem?

This is quite a simple question I have a file with my username in it.
So if I type cat filename, I get felix.whoals
TO show the current process that I am doing, I need to type ps -au felix.whoals

I want to get my username from the filename that I have created.
I was trying with ps -au | cat filename but this gives me an error.

Does anyone know how can I check my current processes by getting my username from the filename instead of entering it manually?

You could do this...
cat felix.whoals | awk '
{
cmd=sprintf("ps -au %s",$1);
print cmd
system (cmd);
}'

So this cannot be solved without awk and sed? I do not want to use these two commands because I want to use the long way to do it. Do you have another solution without using awk?

Programs are run by specifying a name and some optional arguments and additionally some IO redirection.

In the first case you refer to program called "head" with arguments "-1" and "abc".

In the second case you refer to a program called "ps" with arguments "-au", "head", "-1" and "abc".

Do you mean

ps -au `head -1 abc`

so that the output of "head -1 abc" becomes a command line argument?

ps -au `cat filename`

There might be 1000 good ways to do this, but you probably won't find one any better, shorter, or less complicated. The example can be done in one line, just takeout the newlines and the print statement. If you want to print the cmd, the statement probably needs to look like: print(cmd); (notice the parens and semi colon).

yes.

ps -au `head -1 abc` where head -1 abc gives me an argument (username) and then I could use it to get the process running for the current username.

Do you know the way to fix it?

um,

echo $LOGNAME

will give the current user

ps -au `echo $LOGNAME`

would do want you want but can be simplified to

ps -au $LOGNAME

THANKS so much!! You really helped me with SIMPLE and easy code!!