# Terminating a async background job

**URL:** <https://forum.ansible.com/t/terminating-a-async-background-job/11829>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [August 25, 2012, 1:09pm UTC](https://forum.ansible.com/t/terminating-a-async-background-job/11829 "2012-08-25T13:09:17Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [August 25, 2012, 1:09pm UTC](https://forum.ansible.com/t/terminating-a-async-background-job/11829/1 "2012-08-25T13:09:17Z")

</div>

There is a timeout you can set for which the async engine will  
automatically kill the job if it runs longer than that. There is,  
however, no way to stop an async job on demand. Once started, they  
presently complete.

It would require the following changes to support on demand slaying:

(A) In the file that is to be polled by async\_status and is set up by  
async\_wrapper, record the pid of the innermost forked kid (presumably  
as a simple "pid" variable in the JSON file)

(B) Teach async\_status a "terminate=yes|no" option that kills the pid  
set in the job status file from (A). If pid was in the JSON file it  
would kill the process, otherwise, it would return an error that the  
pid was not  
yet recorded. If the status of the job was not marked as running it  
would not try to kill anything.

Patches, provided they stay concise, would be accepted, and probably  
are not that difficult to add.

(Given that terminating a module midstream is not something we  
generally want to encourage, this should not be wired up to  
/usr/bin/ansible or /usr/bin/ansible-playbook as an Interrupt Handler,  
as tempting as that might be -- once a playbook launches a module, I  
want it to complete)

---

<div class="post-metadata">

**Author:** ![Ovais\_Tariq](https://avatars.discourse-cdn.com/v4/letter/o/7ab992/32.png) [@Ovais\_Tariq](https://forum.ansible.com/u/Ovais_Tariq)\
**Post date:** [August 25, 2012, 1:20pm UTC](https://forum.ansible.com/t/terminating-a-async-background-job/11829/2 "2012-08-25T13:20:22Z")

</div>

In my particular scenario, I cannot provide a timeout and wait for it to be exceeded, instead I would like to kill the job on demand. I understand that terminating a job midstream is not to be encouraged, but in certain scenarios this functionality is desirable. Suppose I am running a backup job using ansible on a remote host, there might be some conditions where I would like to be able to kill the job, for example because of disk space issues.

Your suggested changes is what I was thinking of doing as well, and I think I can come up with the patch. Thank you for your reply.
