0

I am using below command to get the pid . "A" is application name and used as parameter. I run my script like ./stop A

PID=`ps -ef | grep A| grep -v grep | awk '{print$2}'`

This is capturing " /bin/ksh ./stop A " process as well .But that I do not want. I only want process of "A" to be captured not my script process in PID/

I have below kill command under my script that is killing my running script :(

kill -9 $PID

It is killing pid of /bin/ksh ./stop A itself also and script is getting stopped at that point .

This is for AIX. How to avoid killing the script itself.

NOTE: When i am working with the same script to do same work but without passing argument it is going good. I believe its obvious as passing an argument to script is creating other pid and for the same application its doing grep and killing.

RonyA
  • 585
  • 3
  • 11
  • 26
  • You could make the `grep` search unique by `-w`, `ps -ef | grep -w "A" | grep -v grep | awk '{print$2}'` ? – Inian Jun 08 '16 at 10:04
  • Not working in AIX . It is still capturing the script process itself and killing it. – RonyA Jun 08 '16 at 10:21

2 Answers2

1

If the stop A is the script from what you are calling the kill, you could do

for p in $PID
do
   test $p -ne $$ && kill -9 $p
done

Note that all other Processes containing an A are still at risk, you might add grep -w A to limit to those where the A is a single word

Edit or without a loop

kill -9 $( ps -ef|grep -w A |grep -v -w $$ | grep -v grep | awk '{print $2}' )

(The grep -v grep should be unneccessary here)

Stefan Hegny
  • 2,107
  • 4
  • 23
  • 26
  • Can we do like this. 1) Capture the pid by PID=`ps -ef | grep A| grep -v grep | awk '{print$2}'` 2) And in the 1st step itself do a -ne $$ ? so that the script pid itself will not save in "PID" – RonyA Jun 08 '16 at 13:26
  • you mean without a loop? But if the outcome of your ps ... is two pids, what then? It's always fulfilling the `-ne` and will kill both – Stefan Hegny Jun 08 '16 at 13:32
  • aaah klickediclick - you mean like `ps -ef|grep -w A |grep -v grep|grep -v -w $$ | awk ... ` I think that should be possible too! not too bad – Stefan Hegny Jun 08 '16 at 13:34
  • Not within loop . I mean only to discard that pid in the line PID=ps -ef | grep A| grep -v grep | awk '{print$2}' itself > This will be really helpful if done and unnecessarily we don't have to put other logic to filter that. – RonyA Jun 08 '16 at 13:35
  • see comment and edit - without loop and pulling the $$ pid out right in the grep's – Stefan Hegny Jun 08 '16 at 13:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114138/discussion-between-rishua-and-stefan-hegny). – RonyA Jun 08 '16 at 14:04
0

There is a utility called pidof which lists PIDs of instances of a program with given name. It's available on most Linux machines, so maybe it's also available on AIX?

From the man page:

Pidof finds the process id's (pids) of the named programs.
It prints those id's on the standard output.

also:

When pidof is invoked with a full pathname to the program
it should find the pid of, it is reasonably safe.
Otherwise it is possible that  it  returns  pids  of running
programs  that  happen to have the same name as the program
you're after but are actually other programs.
Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
  • Not available in AIX. – RonyA Jun 08 '16 at 10:57
  • It is killing right pids unless i am using that script without parameter . But to make it more generic i added the parameter options. After that I am getting this issue. My script is very long and it has do other multiple things related to my application. :( – RonyA Jun 08 '16 at 11:01
  • @RishuA: What is wrong the option I suggested? To help you out further, can you tell me what is the output when you manually run command `ps -ef | grep -w "A" | grep -v grep ` It should return the process-id of only the process `A` – Inian Jun 08 '16 at 11:22
  • @Inian, Let me explain you . When you run any script with parameter it will generate a pid for itself also. As in this case my parameter is A and I am grepping A . So it is capturing pid of my "./script A " also and when we are using -w "A" it will still capture the script pid as the script is run by parameter "A" itself. For reference output : XXX11075780 10879170 0 06:18:11 pts/3 0:00 /bin/ksh ./stop.sh A – RonyA Jun 08 '16 at 11:25
  • Ok, agreed, could use `pgrep`, can you see if `pgrep -f "A"` is returning the application `A`'s pid? because `pgrep` works on `regex`'s. e.g. `pgrep -f "ssh"` – Inian Jun 08 '16 at 11:36
  • As I mentioned I am using AIX , so i have very limited options :) No pgrep available . In freeware I can do with multiple options but AIX is giving me trouble , getting new learning. My environment is AIX and AIX admin manages the OS. I am from application team. – RonyA Jun 08 '16 at 11:40