Let me point out that your command is not quite deterministic.
ps -aef | grep blah | head -2 | tail -1 | awk '{print $2}'

Assuming that there is a command named blah that is running, the ps  
might return:

   UID   PID  PPID   C     STIME TTY           TIME CMD
    99   123     1   0   0:03.56 ??         0:04.83 blah
    99   234   726   0   0:01.99 ??         0:02.36 bash
    99   235   234   0   0:25.36 ??         0:47.66 awk {print $2}
    99   236   235   0   0:04.51 ??         0:06.78 tail -1
    99   237   236   0   0:01.99 ??         0:02.36 head -2
    99   238   237   0   0:25.36 ??         0:47.66 grep blah
    99   239   238   0   0:04.51 ??         0:06.78 ps -aef

grep selects the lines that match blah

    99   123     1   0   0:03.56 ??         0:04.83 blah
    99   238   237   0   0:25.36 ??         0:47.66 grep blah

head -2 returns the first 2 of these lines (i.e., all of them in this  
case)
tail -1 gives the last one:

    99   238   237   0   0:25.36 ??         0:47.66 grep blah

and finally awk '{print $2}' gives

238

BUT, if the blah and grep blah commands had come in the other order in  
the list from ps, then you'd get 123.  Does that help you?

Here's something to try (particularly if the pattern is fixed):

ps -aef | grep -e '[b]lah' | awk '{print $2}'

(or try it with just the ps|grep part and not the awk)

The thing to note is that '[b]lah' does not match itself because it is  
not a 'b' (the only character in the class [b]) followed by 'l', 'a',  
and 'h'. It is a 'b' then a ']' and only THEN 'lah'.

Your problem is not capistrano, but the command that it happens to be  
running.

(and you could just do it all in one command, too:
   run "kill $(ps -aef | grep -e '[b]lah' | awk '{print $2}')"
  but then you wouldn't have learned to fish.)

-Rob

On Sep 18, 2009, at 11:46 AM, pete wrote:

>
> I used Capture, which seems a bit cleaner, I don't have the ability to
> use pkill.
>
> However, I get the wrong PID returned??
>
> When I run my command on the command line manually I get 32553, as an
> example.
>
> When Cap runs, it returns 32106, or something like that.
>
> Why am I getting different PIDs?
>
> On Sep 17, 7:45 pm, Donovan Bray <[email protected]> wrote:
>> Use capture instead of run
>> And parameterize the second command to use what was captured
>>
>> Or investigate pskill and killall that can do it in one step
>>
>> On Sep 17, 2009, at 11:28 AM, pete <[email protected]> wrote:
>>
>>> Let me clarify this a little better...
>>
>>> I would like to do something like this:
>>
>>> task :myTask, :roles => :myhost do
>>>                run "ps- -aef | grep <searchstring> | head -2 | tail
>>> -1 | awk '{print $2}'"
>>>                run "kill <PID FROM PREVIOUS run COMMAND>"
>>> end
>>
>>> On Sep 17, 11:57 am, pete <[email protected]> wrote:
>>>> Hi-
>>
>>>> I want to use Cap to kill a process, but I don't know the PID so  
>>>> I am
>>>> using what I have below to get it:
>>
>>>> ps- -aef | grep <searchstring> | head -2 | tail -1 | awk '{print  
>>>> $2}'
>>
>>>> Is it possible to use the results of the above command in a custom
>>>> task and kill off the PID that is returned?
>>
>>>> Thanks!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Capistrano" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.co.uk/group/capistrano?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to