Unix Script -- Suggestions to list and kill PID's sequentially

Hi,
I'm trying to write a script where i'm trying to grep the PID and the associated file and list them. Then execute the KILL command sequentially on the listed PID's for ".tra" files

====================================================
ps -aux | grep mine

adm   27739  0.2  0.8 1131588 277496 ?  Sl   May29 129:13 /opt/bin/mine  --pid --run --propFile /opt/tib/tra/domain/eng/ABC_Process.tra  --innerProcess
adm   28765  0.8  1.6 1015652 533404 ?  Sl   Jun12 250:24 /opt/bin/mine  --pid --run --propFile /opt/tib/tra/domain/eng/XYZ_Process.tra  --innerProcess
adm   29132  1.6  2.9 1618840 970624 ?  Sl   Jun23 214:49 /opt/bin/mine  --pid --run --propFile /opt/tib/tra/domain/eng/BCY_Archive.tra  --innerProcess
=====================================================

Expecting Output like:

================
adm  27739  ABC_Process.tra
adm  29132   BCY_Archive.tra   

--- Murali.

Try ..

ps -aux | grep mine | nawk '{split($15,b,"/"); print $1,$2,b[7]}'

Not necessary to use grep :slight_smile:

ps -aux | awk '/mine/{...}'
1 Like

Oh My God .. I forgot :smiley:

Better use:

ps aux | awk '/[m]ine{...}'
$ ps -aux | awk '/mine/{split($15,a,"/");print $1,$2,a[length(a)]}'
adm 27739 ABC_Process.tra
adm 28765 XYZ_Process.tra
adm 29132 BCY_Archive.tra

1 Like

Hi All,

The given commands are not working ( I'm using Linux BOX).
Can you please explain me your command. I'm sorry for bugging you guys.
I'm new to this unix world, just want to understand the commands.

$ ps -aux | grep mine | nawk '{split($15,b,"/"); print $1,$2,b[7]}'
-ksh: nawk: not found [No such file or directory]
Warning: bad syntax, perhaps a bogus 

$ ps aux | awk '/[m]ine{...}'
awk: /[m]ine{...}
awk:  ^ unterminated regexp
$

$ ps -aux | awk '/mine/{...}'
awk: /mine/{...}
awk:            ^ syntax error

---------- Post updated at 06:34 PM ---------- Previous update was at 06:21 PM ----------

Hi Raj,

That worked -- Thanks alot

@murali, the greps with ... were not to be taken literally. The ... stands for the code in the curly brackets that was provided by others. You also missed a closing /
The square brackets are needed to keep awk from occasionally matching itself..

1 Like

Hi,

Now I'm able to get the output as required and i can see all the tra files along with the PID's. Please help me to kill the PID's sequentially.
By Manual process when i kill the PID, the log of "ABC_Process.tra" says "Stopped"
How can i Kill each PID sequentially and get the message "Stopped" displayed by the script.

adm 1522 ABC_Process.tra
adm 1939 XYZ_Process.tra
adm 2729 EDB_Archive.tra
$ ps -aux | awk '/mine/{split($15,a,"/");print $1,$2,a[length(a)]}' | while read a PID b; do kill -9 $PID; done

Just a heads up to the OP: that SIGKILL won't let your processes exit gracefully. Unless there's a good reason, better to use the default SIGTERM.

Regards,
Alister

I agree with alister. To put it in other words, better use kill instead of kill -9

Thank you All for your wonderful responses.
I was sick all these days hence was not able to respond.

One last thing i need to know. Hope you to help me on this.

==================================================

I tried killing one PID (27739) manually, the log file read "Terminating" when it killed the PID.

These process will RE-STARTED automatically and when it re-started the corresponding process the log file read "Started"

Can i kill these PID's seqentially using unix script and get the Message -- Terminated / Started, in my script output ?

adm 27739 ABC_Process.tra
adm 28765 XYZ_Process.tra
adm 29132 BCY_Archive.tra

Expecting Output like :

ProcessID Stopped for ABC_Process.tra
Terminated process ABC_Process.tra
Starting process ABC_Process.tra

Similary for the rest. Alternative solutions if any are most welcome.

---------- Post updated at 03:56 PM ---------- Previous update was at 12:48 PM ----------

Raj,

Can you explain me the script you have given

ps -aux | awk '/mine/{split($15,a,"/");print $1,$2,a[length(a)]}'
I searched in forums did not get the concept behind, expect for split Syntax,
split(string,array,separator).

Please let me know how the script is working to get the preferred output

---------- Post updated at 03:59 PM ---------- Previous update was at 03:56 PM ----------

Raj,

Can you explain me the script you have given

ps -aux | awk '/mine/{split($15,a,"/");print $1,$2,a[length(a)]}'
I searched in forums did not get the concept behind, expect for split Syntax,
split(string,array,separator).

Please let me know how the script is working to get the preferred output