# run background job in the shell or script module

**URL:** <https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692>\
**Category:** Ansible Project\
**Created:** [October 16, 2013, 1:54pm UTC](https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692 "2013-10-16T13:54:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shirou](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@shirou](https://forum.ansible.com/u/shirou)\
**Post date:** [October 16, 2013, 1:54pm UTC](https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692/1 "2013-10-16T13:54:22Z")

</div>

Hi all,

I want to launch a script as a background job by using "&".  
First, I try the shell module but it doesn't work (means run as a  
foreground job).

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 16, 2013, 7:51pm UTC](https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692/2 "2013-10-16T19:51:06Z")

</div>

Please read about async mode here:

[http://www.ansibleworks.com/docs/playbooks\_async.html](http://www.ansibleworks.com/docs/playbooks_async.html)

---

<div class="post-metadata">

**Author:** ![shirou](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@shirou](https://forum.ansible.com/u/shirou)\
**Post date:** [October 17, 2013, 3:28pm UTC](https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692/3 "2013-10-17T15:28:50Z")

</div>

it works.  
Thank you!

2013/10/17 4:51 “Michael DeHaan” \<[michael@ansibleworks.com](mailto:michael@ansibleworks.com)\>:

---

<div class="post-metadata">

**Author:** ![Michael\_Slinn](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Michael\_Slinn](https://forum.ansible.com/u/Michael_Slinn)\
**Post date:** [November 25, 2013, 6:38pm UTC](https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692/4 "2013-11-25T18:38:33Z")

</div>

I have a similar problem, and I read the docs, but I must be not seeing something obvious. Here is a fragment of a yaml file ([the entire file is here](https://github.com/mslinn/ansible-ec2-play/blob/master/yaml/launch.yaml)):

- name: Launch project  
command: (sleep 2; /bin/echo “Of course your realize that this means war” \> $home/nohup.out)  
async: 15

poll: 0

There is no output from that step when run with -vv. What am I missing?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 26, 2013, 12:57am UTC](https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692/5 "2013-11-26T00:57:03Z")

</div>

async: 15 will set a “time to live” for 15 seconds to the task.

Poll: 0 means “fire and forget, I don’t want any output”.

Sounds like you might want to do poll: 5 or so.

It also sounds like your project might benefit from a service init script, and then you can just use the service module.

---

<div class="post-metadata">

**Author:** ![Michael\_Slinn](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Michael\_Slinn](https://forum.ansible.com/u/Michael_Slinn)\
**Post date:** [November 26, 2013, 12:57am UTC](https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692/6 "2013-11-26T00:57:34Z")

</div>

I just want to kick off a background process. I don’t want any output.  
I thought that [Service](http://www.ansibleworks.com/docs/modules.html#service) only pertained to system services. Can I use them for background tasks also?

Thanks,

Mike

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 26, 2013, 1:08am UTC](https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692/7 "2013-11-26T01:08:21Z")

</div>

So contradictory statements:

“There is no output from that step when run with -vv. What am I missing?”

“I just want to kick off a background process. I don’t want any output.”

Which do you want and what are you getting versus what you are expecting?

“I thought that [Service](http://www.ansibleworks.com/docs/modules.html#service) only pertained to system services. Can I use them for background tasks also?”

Yes, you can write system init scripts to do basically anything.

You might also be interested in something like supervisor, which we also have a module for.

---

<div class="post-metadata">

**Author:** ![Michael\_Slinn](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Michael\_Slinn](https://forum.ansible.com/u/Michael_Slinn)\
**Post date:** [November 26, 2013, 2:19am UTC](https://forum.ansible.com/t/run-background-job-in-the-shell-or-script-module/13692/8 "2013-11-26T02:19:41Z")

</div>

I defined a system service and it works. Thank you!
