LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Listing users and processes. (https://www.linuxquestions.org/questions/linux-newbie-8/listing-users-and-processes-452540/)

mike9287 06-07-2006 05:05 PM

Listing users and processes.
 
Hi,

Is there a way to display users logged on and there processes on one print rather than using "ps -a" and "finger" and then merging the relavent fields with AWK?

Cheers

Mike

twantrd 06-07-2006 06:02 PM

Run 'w'. Does it display what you want?

-twantrd

osor 06-07-2006 10:20 PM

Never mind.

mike9287 06-08-2006 02:39 AM

Quote:

Originally Posted by osor
Never mind.

Uhh?

Do you have any idea if this is possible?

timmeke 06-08-2006 02:47 AM

I suggest using "w" or "who" rather than "finger".
ie
Code:

who -up

mike9287 06-08-2006 02:54 AM

Quote:

Originally Posted by timmeke
I suggest using "w" or "who" rather than "finger".
ie
Code:

who -up


Nice one, thanks. Will check once I get home.

Mike

mike9287 06-08-2006 04:47 PM

Hi again,

I can't seem to use the who -p option on my shell but I have found that if I type "ps -U" username then it lists the processes for that user, so If I can somehow extract the user names from who and then send then to "ps -U" one at a time then I should be okay, only trouble is I can't work out how to do it, it won't let me pipe the info.

Any ideas?

Cheers

mike

mike9287 06-08-2006 05:29 PM

Hi,

I think I have it sorted, I need to find out how to make awk print " quotations marks, i tried preceding them with a backslash but it dont work, does anyone if its possible?

Mike

EDIT its okay, i got it sorted without them :)

I needed to write a program which displayed users and there processes plus there TTY and time and came up with this :

Code:


#!/bin/sh
finger > myfile
awk '{print $1, $2, $3}' myfile |
sed -e 's/Login Name Tty//g' |
awk '{print "echo " $2, $3} {print "ps -U ",$1} ' |
sed -e '1s/echo//g;2s/ps -U//g' > myfile
sh myfile

this works fine I now get

Code:


michael kelly
  PID TTY          TIME CMD
22339 ?        00:00:01 sshd
22340 pts/1    00:00:00 bash
30515 pts/1    00:00:00 sh
30521 pts/1    00:00:00 sh
30522 pts/1    00:00:00 ps
[michael.kelly@unix michael.kelly]$

What do you reckon? any ways I might look to improve it? I dont need the seconds sed commadn if I can stop awk's print method planting an extra echo and ps -U at the top of my script with no arguments beside them. I get an error but the rest of the scripts run so I ahev included the seconds sed to delete the lines before I call "sh" to execute.




Mike

timmeke 06-09-2006 01:57 AM

A hint:
the problem you describe lies in the fact that the first line printed by "finger" is just a column header.
So, modify your first awk to disregard this header row and you should be fine.

Code:

awk '{print $1, $2, $3}' myfile |
thus should be replaced by:
Code:

awk {if (NR>1) print $1, $2, $3}' myfile |
or something like that.

Tinkster 06-09-2006 02:12 AM

Not sure what your EXACT intention is, but ...

Code:

ps -au | grep `who|cut -d" " -f1`
[edit]
OK, that was daft; doesn't work if there's more than
one user logged in :} ... I'll think some more.
[/edit]

Ok, that works :}
[edit2]
Code:

ps -au|egrep `who|awk '{printf $1"|"}END{print""}'|sed 's/|$//g'`
[/edit2]


Cheers,
Tink

mike9287 06-09-2006 02:14 AM

Quote:

Originally Posted by timmeke
A hint:
the problem you describe lies in the fact that the first line printed by "finger" is just a column header.
So, modify your first awk to disregard this header row and you should be fine.

Code:

awk '{print $1, $2, $3}' myfile |
thus should be replaced by:
Code:

awk {if (NR>1) print $1, $2, $3}' myfile |
or something like that.

Thanks, that sound good and will half the length of my script with adding 3 characters.

That also conculdes the course work I have to do so I won't be bothering you anymore (at least not for a while). I must say a big thank you to everyone who helped.

Thanks m8

Mike


All times are GMT -5. The time now is 12:55 PM.