Operating System - HP-UX
1833300 Members
3036 Online
110051 Solutions
New Discussion

Re: Kill all processes PHN8 in group where user belongs to

 
SOLVED
Go to solution
tom quach_1
Super Advisor

Kill all processes PHN8 in group where user belongs to

Hi all,


ps- ef |grep PHN8 |awk '{print $2}' |xargs /usr/local/bin/sudo kill -9

This script will kill all processes with the name PHN8.
i would like to modify this script to search for user primary group and only kill
processes that particular user who run the script belong to.
Thanks for your help.
Regards,
Tom
13 REPLIES 13
TwoProc
Honored Contributor

Re: Kill all processes PHN8 in group where user belongs to

Well for the "group" part - add the use of the "-g" switch of the ps command.

ps -efg "group" | grep ...

and for the user part...

ps -efu "user" | grep...

also consider

ps -ef -C PHN8 -g "group" -u "user"

should limit you to a select group of users in a certain command group running PHN8 all in one command.
We are the people our parents warned us about --Jimmy Buffett
Bill Hassell
Honored Contributor

Re: Kill all processes PHN8 in group where user belongs to

Start by never using grep. grep isn't particular about what it matches so you could find a process like: myproc 8PHN89 and it would match--not a good thing. Start by using all of the ps match options. To do this, you need to turn on the UNIX965 variable just for ps, something like this:

UNIX95= ps -fC PHN8

Now notice that this always finds processes that are exactly "PHN8", no more and no less. Then look at the -g and -u options to further limit the search.

Finally, do NOT use kill -9 as your kill signal. It will always cause problems. Use kill -15 to allow processes to terminate normally and cleanup correctly.

And don't be tempted to seet UNIX95 for everything as in: export UNIX95=. It will change other commands and libraries. Just use as shown above. NOTE: To see the hierarchy of processes (parent/chile), use the -H option with UNIX95=.


Bill Hassell, sysadmin
tom quach_1
Super Advisor

Re: Kill all processes PHN8 in group where user belongs to


Thanks for your help.

there are 2 group: IT and PROD
how can i get the process with user belongs to IT group
Thanks,
Tom



#UNIX95= ps -fC PH8SRVN -g IT
ps: wrong session number IT
UID PID PPID C STIME TTY TIME CMD
chis 21571 21569 0 17:31:57 ? 00:00 /opt/cognos/ph843d/bin/PH8SRVN
regina 16961 16959 0 17:06:05 ? 00:00 /opt/cognos/ph843d/bin/PH8SRVN
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Kill all processes PHN8 in group where user belongs to

I think you have to give the numeric id of your group.
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: Kill all processes PHN8 in group where user belongs to

Try as,

#UNIX95= ps -fC PH8SRVN -G IT

hth.
Easy to suggest when don't know about the problem!
tom quach_1
Super Advisor

Re: Kill all processes PHN8 in group where user belongs to

Thanks again for all your input.

below are ids for users "alb" and "chis"
user alb gid=110(IT)
user chis gid=105(china

the commands below should return processes belong to group china ONLY but it returned both group china and IT.
Please how can i fix this.

Regards,
Tom

#UNIX95= ps -fC PH8SRVN -g china
#UNIX95= ps -fC PH8SRVN -G china

#id alb
uid=104(alb) gid=110(IT) groups=20(users),103(dba),105(china)
sunuxap1:[root]:/
#id chis
uid=163(chis) gid=105(china) groups=20(users),110(IT)

#UNIX95= ps -fC PH8SRVN -g china
ps: wrong session number china
UID PID PPID C STIME TTY TIME CMD
chis 23045 23043 0 07:48:53 ? 00:00 /opt/cognos/ph843d/bin/PH8SRVN
albany 23433 23431 0 07:51:09 ? 00:00 /opt/cognos/ph843d/bin/PH8SRVN


#UNIX95= ps -fC PH8SRVN -G china
UID PID PPID C STIME TTY TIME CMD
albany 29214 29212 0 08:25:16 ? 00:00 /opt/cognos/ph843d/bin/PH8SRVN
chis 29132 29130 0 08:24:51 ? 00:00 /opt/cognos/ph843d/bin/PH8SRVN
chis 29130 1593 0 08:24:51 ? 00:00 sh -c /opt/cognos/ph843d/bin/PH8INIT /opt/cognos/ph843d/bin/PH8


TwoProc
Honored Contributor

Re: Kill all processes PHN8 in group where user belongs to

Tom, it looks like the ps command is doing a logical "or" function instead of a logical "and" function for the command and the group. That's so bad, you'd swear it was a bug.

So now your problem gets into lots of scripting...
We are the people our parents warned us about --Jimmy Buffett
Ermin Borovac
Honored Contributor
Solution

Re: Kill all processes PHN8 in group where user belongs to

From ps man page

If more than one of the -C, -g, -G, -p, -R, -t, -u, -Z, and -U options
are specified, processes will be selected if they match any of the
options specified.

Why don't you try something like

$ UNIX95= ps -o user,group,pid,args -C PHN8 | awk '$2 == "china" {print $0}'
tom quach_1
Super Advisor

Re: Kill all processes PHN8 in group where user belongs to


Thanks-Ermin
that is what i am looking for
it works very well
$ UNIX95= ps -o user,group,pid,args -C PHN8 | awk '$2 == "china" {print $0}'

One more question would like to ask:
if i want to replace group "china" with a variable as the line below. This line gives me the group name, when plug in together.
something is not correct.
id |cut -f2 -d" "|cut -f2 -d"("|cut -f1 -d")"


#!/usr/bin/sh
ID=/usr/bin/id
CUT=/usr/bin/cut
GPNAME=`$ID |$CUT -f2 -d" "|$CUT -f2 -d"("|$CUT -f1 -d")"`
UNIX95= ps -o user,group,pid,args -C PH8SRVN |awk '$2=="$GPNAME" {print $0}'

the $GPNAME of the last line did not pickup the variable return from the line above.
Can anyone please help?


Thank you very much,
Tom
Bill Hassell
Honored Contributor

Re: Kill all processes PHN8 in group where user belongs to

Your script can be traced with the -x option. Either run the script like this:

sh -x myscript

Or insert set -x as the second line in the script. However, your script can be simplified quite a bit:

#!/usr/bin/sh
export PATH=/usr/bin

GPNAME=$(id|awk '{print $2}'|tr -s "(" " "|cut -f2 -d=|awk '{print $1}')

UNIX95= ps -o user,group,pid,args -C PH8SRVN -G $GPNAME

The `cmds` construct is considered deprecated (see man sh-posix or man ksh) in favor of $(cmds). There are several other techniques to extract the group information from id. In this example, the group number is extracted rather than the name. For ps, the -G option accepts a GID number or symbolic name. And since ps can search for the GID, an additional string compare by awk isn't necessary.


Bill Hassell, sysadmin
Ermin Borovac
Honored Contributor

Re: Kill all processes PHN8 in group where user belongs to

Here's one way to introduce shell variables into awk statements.

Change this

UNIX95= ps -o user,group,pid,args -C PH8SRVN |awk '$2=="$GPNAME" {print $0}'

to

UNIX95= ps -o user,group,pid,args -C PH8SRVN |awk -v GROUP=$GPNAME '$2==GROUP {print $0}'
tom quach_1
Super Advisor

Re: Kill all processes PHN8 in group where user belongs to

Thank you very much Bill & Ermin
It works great

Regards,
Tom
tom quach_1
Super Advisor

Re: Kill all processes PHN8 in group where user belongs to

Thank you!
Regards,
Tom