Script to kill process

Hello guys,

I have a process named monitoreo, with 'monitoreo start' my process start until i kill them, now i want to do 'monitoreo stop' to kill them.

After 'monitoreo start' i have this process running:

ps -af
     UID   PID  PPID  C    STIME TTY      TIME CMD
     ati 10958  1495  0 15:14:19 pts/2    0:00 sleep 5
     ati 10959 27456  0 15:14:21 pts/2    0:00 ps -af
     ati  1495     1  0 14:59:43 pts/2    0:01 /bin/bash ./monitoreo start

no i want to kill it with monitoreo stop, i need to save the pid of 'monitoreo start' to kill them later, how i can do it?

I try with

ps -aef | grep monitoreo | grep -v grep | cut -d" " -f8 > pid_file

then i try to kill -9 the cat of pid_file but somethimes is -f8 and sometimes is -f7 cuz there are spaces.... that have sence?

#!/bin/ksh

kill $(ps -ef | nawk '/monitoreo start/ { print $2}'}

There are two problems with this line:

  1. by using double quotes to surround the space in the '-d'-option of "cut" you don't guard this space from shell expansion in every case. Maybe this is not causing you problems right now, but better to use single quotes: ... | cut -d' ' ...

  2. you use a single space as a field delimiter. By looking at the output of 'ps' you will notice that it is in tabbed format, that is: with more than one blank separating the fields. Either use 'cut' with the '-c' (column) option (not recommendable) or change the occurrance of one or more spaces to one space before:

... | sed 's/<space><space>*/<space>/g' | cut -d' ' ...

replace the <space> to real space characters above.

Hope this helps

bakunin

Try this,

kill -9 `ps -aef | grep monitoreo | grep -v grep | awk '{print $2}'`

This will avoid problems of spaces and delimiters as awk itself takes care.

Check it out.
:cool:

true - you forgot to mention, though, that it uses up lots of system resources in doing so.

What one thinks about wasting resources is left to everybodies judgement.

bakunin

Amit, your post help me, I just have to add some characters and at the end the solution was:

kill -9 `ps -aef | grep 'monitoreo start' | grep -v grep | awk '{print $2}'`

Thanx