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

2,303 views
Skip to first unread message

ZillaYT

unread,
Jan 3, 2018, 2:55:53 PM1/3/18
to Ansible Project
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 &




Toshio Kuratomi

unread,
Jan 3, 2018, 3:54:23 PM1/3/18
to ansible...@googlegroups.com
Or option three: use async:. 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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/70316e05-1357-40d8-aab4-53092dd8edc1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ZillaYT

unread,
Jan 3, 2018, 4:17:19 PM1/3/18
to Ansible Project
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.


On Wednesday, January 3, 2018 at 3:54:23 PM UTC-5, Toshio Kuratomi wrote:
Or option three: use async:. 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
On Jan 3, 2018 11:56 AM, "ZillaYT" <zil...@gmail.com> wrote:
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 &




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
Jan 5, 2018, 5:39:31 AM1/5/18
to ansible...@googlegroups.com
On 03.01.2018 20:55, ZillaYT wrote:
> Say I want to run a script in the background after my Ansible run
> finishes.
> Which of these two options is the correct way?

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

--
Kai Stian Olstad

ZillaYT

unread,
Jan 5, 2018, 10:55:49 AM1/5/18
to Ansible Project
Thanks. I'll also look at synchronized application.

ZillaYT

unread,
Jan 5, 2018, 10:58:35 AM1/5/18
to Ansible Project
Sorry, I meant supervisord as another means to start a deamon.
Reply all
Reply to author
Forward
0 new messages