Killing processes but not failing if they aren't there

6,442 views
Skip to first unread message

Lorin Hochstein

unread,
Oct 7, 2012, 11:46:46 PM10/7/12
to ansible...@googlegroups.com
I'm trying to kill a bunch of processes using an ansible task, but I don't want the task to fail if there are no processes of interest.

I created a task that looks like this:

    - name: kill websocket server
      action: shell pgrep -f websockify | xargs kill || true

The problem is that this returns with a -15 return value, despite the || true at the end. Here's what it looks like if I use the ansible command:

    $ ansible vnc -m shell -a "pgrep -f websockify | xargs kill || true" -s
    192.168.108.102 | FAILED | rc=-15 >>

Anybody know why I'd get a non-zero return code?  If I just run the command as root, when it fails, I do get the 0 return value:

root@precise64:~# pgrep -f websockify | xargs kill || true
Usage:
  kill pid ...              Send SIGTERM to every process listed.
  kill signal pid ...       Send a signal to every process listed.
  kill -s signal pid ...    Send a signal to every process listed.
  kill -l                   List all signal names.
  kill -L                   List all signal names in a nice table.
  kill -l signal            Convert between signal numbers and names.
root@precise64:~# echo $?
0

Here's ansible version deatils

$ ansible --version
ansible 0.8 (devel c8854a23ea) last updated 2012/10/07 21:16:36 (GMT -400)

Dag Wieers

unread,
Oct 8, 2012, 3:39:32 AM10/8/12
to ansible...@googlegroups.com
On Sun, 7 Oct 2012, Lorin Hochstein wrote:

> I'm trying to kill a bunch of processes using an ansible task, but I don't
> want the task to fail if there are no processes of interest.
>
> I created a task that looks like this:
>
> - name: kill websocket server
> action: shell pgrep -f websockify | xargs kill || true

I would go for:

- name: kill websocket server
action: shell pkill -f websockify
ignore_errors: True


> Anybody know why I'd get a non-zero return code? If I just run the command
> as root, when it fails, I do get the 0 return value:

Might be a bug worth fixing, although I cannot reproduce this:

----
- hosts: all
gather_facts: False

tasks:
- name: 'Testing shell module #1'
action: shell true | false || true
- name: 'Testing shell module #2'
action: shell false | false || true
- name: 'Testing shell module #3'
action: shell false | true || true
----

gives me:

----
[root@moria ansible]# ansible-playbook -l localhost test9.yml

PLAY [all] *********************

TASK: [Testing shell module #1] *********************
changed: [localhost]

TASK: [Testing shell module #2] *********************
changed: [localhost]

TASK: [Testing shell module #3] *********************
changed: [localhost]


PLAY RECAP *********************
localhost : ok=3 changed=3 unreachable=0 failed=0
----

Kind regards,
--
-- dag wieers, d...@wieers.com, http://dag.wieers.com/
-- dagit linux solutions, in...@dagit.net, http://dagit.net/

[Any errors in spelling, tact or fact are transmission errors]

Michael DeHaan

unread,
Oct 8, 2012, 7:24:34 AM10/8/12
to ansible...@googlegroups.com
>
> I would go for:
>
>
> - name: kill websocket server
> action: shell pkill -f websockify
> ignore_errors: True

+1


>
>> Anybody know why I'd get a non-zero return code? If I just run the
>> command
>> as root, when it fails, I do get the 0 return value:


No idea, doesn't seem to be an ansible thing and may be related to
your invocation in that particular instance?

In any event, should you wish to debug further, I'm interested in the results.

Lorin Hochstein

unread,
Oct 8, 2012, 8:15:28 AM10/8/12
to ansible...@googlegroups.com
I'll try to do some digging to figure out what's going on here.


Take care,

Lorin
--
Lorin Hochstein
Lead Architect - Cloud Services
Nimbis Services, Inc.

Brian Coca

unread,
Oct 8, 2012, 2:53:43 PM10/8/12
to ansible...@googlegroups.com

Give xargs a -r, wont execute with error on empty args

Reply all
Reply to author
Forward
0 new messages