I am looking for a unix command or a bash script to get the processes currently run by users in the system.
I tried the ps -Af
command, however, all the processes including the root processes pop up.
Is there any command to get the processes run only by user? Otherwise I need some help in writing a bash script to take out the root processes from the ps -Af
command.
-
pipe the results through grep.– Preet SanghaNov 14, 2013 at 23:33
-
@Preet: For that to be useful, you'd need to show a way to check just the username and not match filenames and commander-line arguments.– Ben VoigtNov 14, 2013 at 23:41
-
Question belongs on Super User or Unix & Linux– Stephen PNov 15, 2013 at 2:06
6 Answers
If you want to remove root
, just do your ps -Af
and pipe it though grep:
ps -Af | grep -v root
Of course, this is sloppy. If you're running a program called troot, you'll lose that as well. You can be more precise using awk
and removing any line that has root in the right column. I don't have the output from ps -Af
in front of me, but for the sake of argument, let's assume the userid is in the 3rd column. In that case, you'd want to do this:
ps -Af | awk '$3 != "root" { print $0 }'
-
1On my system the user is in the first column, so a simple
ps -Af | grep -v '^root'
would do.– pfnueselNov 15, 2013 at 0:06
how about ps -U <username>
(works on MacOSX) or pipe through grep
ps -Af | grep -v 'root'
should show all processes not owned by root. But beware, grep will not be limited to the username. A process exposing some path containing "root" will also be removed from the result.
You shoud try
whowatch
It gives you a nice overview of which users are logged in, which processed they currently run and so on