Scripting - process and user ids...Help please


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Scripting - process and user ids...Help please
# 1  
Old 03-02-2010
Scripting - process and user ids...Help please

Hello all:

Working on a job I was asked get a simple script to perform the following task and would like to ask for some help. I'm looking forward to learning more and diving deeper into the World of Open Source servers.

I need a script for a Unix server, using as few lines as possible, that generates a list of users that have active processes on a server and the number of processes for each of those users.

I know the basic scripting and syntax of creating structured code creation as I had to take a bunch of programming classes in college, and have created many batch apps in the command line world.
I'm assuming there is a show process command, and for some reason I want to say it's something like sho usr or close to that.
If it was commented out that would be really great too.

Would someone please be so kind to assist me or have an old one lying around?
Many thanks in advance for your consideration,
Mo-
# 2  
Old 03-02-2010
MySQL Solution

Try this ,

Code:
echo -e "Name\t\tNo of Process"
for name in `users | tr ' ' '\n' |uniq`
do
        count=`ps -U $name | wc -l`
        let count=$count-1
        echo -e "$name \t\t $count"
done

# 3  
Old 03-02-2010
Try:

Code:
ps -ef | awk 'NR>1{ print $1 }' | sort | uniq -c

Sample output:

Code:
   4 aja1
   8 root
   2 stu1


Last edited by dennis.jacob; 03-02-2010 at 02:24 AM..
# 4  
Old 03-02-2010
Thanks so much!

Here is what I was coming up on my own.

I understand most of what is happening, however could you let me know If I'm interpreting your example correctly?


ps, by itself, shows you information about only your processes. To learn more about all of the processes that are running, use the -ef options with the ps command.
Code:
$ ps -ef UID   PID  PPID  C    STIME TTY      TIME COMD
root     0     0  0   Mar 18 ?        0:01 sched
root     1     0  0   Mar 18 ?        1:09 /sbin/init
root   286   279  0   Mar 23 ?        0:08 /usr/lib/saf/ttymon
root   279     1  0   Mar 23 ?        0:04 /usr/lib/saf/sac -t 300
root   262     1  0   Mar 23 ?        0:02 /usr/X/bin/xdm
aja1 11687   262  0 16:37:28 ?        1:10 /usr/X/bin/xdm
aja1 17764 11687  0 07:57:51 ?        0:03 -wksh
root   288   279  0   Mar 23 ?        0:05 /usr/lib/saf/ttymon
aja1 17867 17866  0 08:08:47 pts/2    0:04 wksh
root   344     1  0   Mar 23 ?        0:01 /usr/lib/lpsched
root   326     1  0   Mar 23 ?        5:25 /usr/sbin/cron
aja1 18333 18154  0 09:03:40 pts/3    0:00 wksh
stu1 19305 19286 31 10:58:19 pts/4    0:01 ps -ef
stu1 19207 17867  0 10:52:50 pts/2    0:01 -ksh
$ _}

I understand most of what is happening, however could you let me
know If I'm interpreting your example correctly?

Can you show me an example of your output? I'm at home and can't try it myself. I would be very appreciative.
I realize I'm being a pain, however If you can I'd be very happy.

Many thanks,
Mo-

---------- Post updated at 10:32 PM ---------- Previous update was at 10:16 PM ----------

If I was going to be contructing this in good form:

I thought it might look something like what I started below and commented out so even a newby like me can quickly look at it and see what is happening.

I like to have the output be formatted in a manner that is a little more self explanitory.

Can you let me know if I'm on the right track? Am I missing something fundamental? Am I doing something stupid? All assistance is most welcome.
Thanks,


Code:
#!/bin/bash 

# Title........: serv_u_p
# Description..: Displays Server user and processes
# Author.......: Mo Ahten - TBD 
# Contact......: moahten_____
# Last Modified: 03/01/10 
#----------------------------------
 
# Begin script
$ ps –ef >> serv_u_p.txt
# End Script


Last edited by pludi; 03-02-2010 at 02:41 AM.. Reason: code tags, please!!!
# 5  
Old 03-02-2010
MySQL

This is how my script works,

Code:
   echo -e "Name\t\tNo of Process"
for name in `users | tr ' ' '\n' |uniq`
do
        count=`ps -U $name | wc -l`
        let count=$count-1
        echo -e "$name \t\t $count"
done

I get the users who have the processes by the command sequence "users | tr ' ' '\n' |uniq".The command "users" will give the user name with some process.Then I align the output and get the result as all the users.

like ,

karthigayan
aaa
bbb
ccc
....
....

The using the user name I get the number of process running for the users by using the "ps -U $name | wc -l".

The reason for removing the count is by one is that the ps will give the output with its header.

Here is the output ,

Code:
     Name                       No of Process
     karthigayan              43
     aaa                             36
     bbb                             57
     ccc                              39
     ....
     ....

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List out Process ids restarted today

Hi, I need to list out the processes which are started/restarted today in my Solaris box. If not possible need to convert the process uptime in minutes or seconds and compare it with a configurable value to list out those process ids for further processing in my scripting. Can any one guide... (7 Replies)
Discussion started by: ananan
7 Replies

2. Shell Programming and Scripting

Search process ids

Hi, I have four tomcat instances named PNK, PNK1, PNK2, PNK3. All are running on the same server. To kill tomcat instance I usually do below for PNK1,PNK2,PNK3 kill -9 `ps -ef|grep tomcat|grep PNK1|grep -v grep|awk '{print $2}'` But the above command does not work for PNK. Can some... (7 Replies)
Discussion started by: lpprasad321
7 Replies

3. AIX

Create a bundle of user ids

if I want to create a bundle of user ids on some aix servers, if there is a way not need to do "passwd username" one by one user to set the password? Thanks (3 Replies)
Discussion started by: rainbow_bean
3 Replies

4. Shell Programming and Scripting

ps returning more process ids

Please help me with this question I have a tantan.sh under /home/mydir which is a caller to another script "tantan.sh" under /home/anotherdir ----------------------------------------------------------- /home/mydir/tantan.sh ------------------------------------------------------------... (6 Replies)
Discussion started by: guruincredible
6 Replies

5. Shell Programming and Scripting

Script for to kill the process Ids when restarting the unix server

Hi, I need a script to kill the process Ids for the user ABC. I prepared the following script after that while logging with user therough script i am not sure how to pass the user name and password.Can ou modify the script and help me out. #!/bin/bash for filesize in $(ls -ltr | grep... (4 Replies)
Discussion started by: victory
4 Replies

6. AIX

Process ids consuming huge resources ?

Hi All what is the command to check process ids , which are running from long time and which are consuming more cpu? Also how to check, what a particular PID is running what For Ex: i have a pid :3223722 which is running since from long time, if i want to check what is this... (1 Reply)
Discussion started by: sidharthmellam
1 Replies

7. Shell Programming and Scripting

script to loop all process ids and take pmap

Hi all, I need a script that will loop around all the current processes and take a pmap -x <process id> and output each pmap to a separate file. Would anyone have a quick command to do this? (2 Replies)
Discussion started by: borderblaster
2 Replies

8. Shell Programming and Scripting

validate user ids

Hi I have to validate the user ids. It should be numeric. I am using following code echo $input | grep '^\{11\} > /dev/null if echo "error" else echo "Success" fi But when i entered user id as 828^&% the output is 8565 8566 -bash: ^: command not found Means when i entered... (4 Replies)
Discussion started by: KiranKumarKarre
4 Replies

9. UNIX for Advanced & Expert Users

Problem to track process IDs in HP-UX machine

Hello All, I need to track the exact process ID related to a particular application running on HP-UX machine. i.e. the exact functionality of fuser unix command. fuser application.log // It gives the exact process ID for application.log Since there is no provision for 'fuser' in HP-UX... (13 Replies)
Discussion started by: abhishek0071
13 Replies

10. Programming

List of Thread IDs of a process

Hello, Can some one tell how to read the thread IDs of the current process in Sun Solaris. Any help will be appreciated. regards, Murali (0 Replies)
Discussion started by: hmurali
0 Replies
Login or Register to Ask a Question