# In an Ansible task, what is the proper way to run a script in the background?

**URL:** <https://forum.ansible.com/t/in-an-ansible-task-what-is-the-proper-way-to-run-a-script-in-the-background/26410>\
**Category:** Ansible Project\
**Created:** [January 3, 2018, 7:55pm UTC](https://forum.ansible.com/t/in-an-ansible-task-what-is-the-proper-way-to-run-a-script-in-the-background/26410 "2018-01-03T19:55:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 3, 2018, 7:55pm UTC](https://forum.ansible.com/t/in-an-ansible-task-what-is-the-proper-way-to-run-a-script-in-the-background/26410/1 "2018-01-03T19:55:53Z")

</div>

Say I want to run a script in the background after my Ansible run finishes. Which of these two options is the correct way?

Option 1: Run a nohup on the Ansible task itself

`

- name:Run a script in the background  
command: nohup myscript.sh 2\>&1 &  
`

where myscript.sh does something (anything) but has no nohup calls.

Option 2: Put the nohup in the script itself?

`

- name: Run a script in the background  
command: ./myscript.sh  
`

and myscript.sh has in it the nohup

myscript.sh

`nodup do_something 2>&1 &`

---

<div class="post-metadata">

**Author:** ![tkuratom](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@tkuratom](https://forum.ansible.com/u/tkuratom)\
**Post date:** [January 3, 2018, 8:53pm UTC](https://forum.ansible.com/t/in-an-ansible-task-what-is-the-proper-way-to-run-a-script-in-the-background/26410/2 "2018-01-03T20:53:57Z")

</div>

Or option three: use async:. [http://docs.ansible.com/ansible/latest/playbooks\_async.html](http://docs.ansible.com/ansible/latest/playbooks_async.html)

If the script is something you want to run and exit after performing its tall, I think async is the most idiomatic. If the script is more like a daemon, having nohup in the script feels right to me but I can’t think of a reason either would be better.  
-Toshio

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 3, 2018, 9:17pm UTC](https://forum.ansible.com/t/in-an-ansible-task-what-is-the-proper-way-to-run-a-script-in-the-background/26410/3 "2018-01-03T21:17:18Z")

</div>

Thanks, I didn’t know about async, but it’s NOT what I want since I do want the script to run as a daemon indeed.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 5, 2018, 10:39am UTC](https://forum.ansible.com/t/in-an-ansible-task-what-is-the-proper-way-to-run-a-script-in-the-background/26410/4 "2018-01-05T10:39:03Z")

</div>

I would say none of them.  
Create a init file for you init system and start the program with the service module.

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 5, 2018, 3:55pm UTC](https://forum.ansible.com/t/in-an-ansible-task-what-is-the-proper-way-to-run-a-script-in-the-background/26410/5 "2018-01-05T15:55:49Z")

</div>

Thanks. I’ll also look at synchronized application.

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 5, 2018, 3:58pm UTC](https://forum.ansible.com/t/in-an-ansible-task-what-is-the-proper-way-to-run-a-script-in-the-background/26410/6 "2018-01-05T15:58:35Z")

</div>

Sorry, I meant supervisord as another means to start a deamon.
