# Killing processes but not failing if they aren't there

**URL:** <https://forum.ansible.com/t/killing-processes-but-not-failing-if-they-arent-there/11957>\
**Category:** Ansible Project\
**Created:** [October 8, 2012, 3:46am UTC](https://forum.ansible.com/t/killing-processes-but-not-failing-if-they-arent-there/11957 "2012-10-08T03:46:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [October 8, 2012, 3:46am UTC](https://forum.ansible.com/t/killing-processes-but-not-failing-if-they-arent-there/11957/1 "2012-10-08T03:46:46Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [October 8, 2012, 7:39am UTC](https://forum.ansible.com/t/killing-processes-but-not-failing-if-they-arent-there/11957/2 "2012-10-08T07:39:32Z")

</div>

> 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:
> 
> &nbsp;&nbsp;&nbsp;- name: kill websocket server  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: shell pgrep -f websockify | xargs kill || true

I would go for:

&nbsp;&nbsp;&nbsp;- name: kill websocket server  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: shell pkill -f websockify  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 8, 2012, 11:24am UTC](https://forum.ansible.com/t/killing-processes-but-not-failing-if-they-arent-there/11957/3 "2012-10-08T11:24:34Z")

</div>

> I would go for:
> 
> &nbsp;&nbsp;- name: kill websocket server  
> &nbsp;&nbsp;&nbsp;&nbsp;action: shell pkill -f websockify  
> &nbsp;&nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [October 8, 2012, 12:15pm UTC](https://forum.ansible.com/t/killing-processes-but-not-failing-if-they-arent-there/11957/4 "2012-10-08T12:15:28Z")

</div>

I’ll try to do some digging to figure out what’s going on here.

Take care,

Lorin

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [October 8, 2012, 6:53pm UTC](https://forum.ansible.com/t/killing-processes-but-not-failing-if-they-arent-there/11957/5 "2012-10-08T18:53:43Z")

</div>

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