# Run daemon on a server

**URL:** <https://forum.ansible.com/t/run-daemon-on-a-server/11729>\
**Category:** Ansible Project\
**Created:** [July 26, 2012, 7:24pm UTC](https://forum.ansible.com/t/run-daemon-on-a-server/11729 "2012-07-26T19:24:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![paweu](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@paweu](https://forum.ansible.com/u/paweu)\
**Post date:** [July 26, 2012, 7:24pm UTC](https://forum.ansible.com/t/run-daemon-on-a-server/11729/1 "2012-07-26T19:24:07Z")

</div>

Hi,

I’m trying to run a daemon on a server with ansible:

- name: “Run daemon”  
action: shell if [-z “$(ps axu | grep daemon | grep -v grep)”]; then /root/bin/daemon; fi

I would like it to turn the daemon on and run the next action in my playbook, but it is stuck there as if it cannot let the daemon go to background. I tried & at the end already

When just starting from the shell this daemon goes straight to background.

---

<div class="post-metadata">

**Author:** ![Mark\_Theunissen](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@Mark\_Theunissen](https://forum.ansible.com/u/Mark_Theunissen)\
**Post date:** [July 26, 2012, 7:26pm UTC](https://forum.ansible.com/t/run-daemon-on-a-server/11729/2 "2012-07-26T19:26:02Z")

</div>

Could you make an init script for it? Have you tried using the ‘service’ module instead of ‘shell’?

service name=mydaemon state=started

[http://ansible.github.com/modules.html#id11](http://ansible.github.com/modules.html#id11)

---

<div class="post-metadata">

**Author:** ![paweu](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@paweu](https://forum.ansible.com/u/paweu)\
**Post date:** [July 26, 2012, 7:31pm UTC](https://forum.ansible.com/t/run-daemon-on-a-server/11729/3 "2012-07-26T19:31:12Z")

</div>

W dniu czwartek, 26 lipca 2012 21:26:02 UTC+2 użytkownik Mark Theunissen napisał:

> Could you make an init script for it? Have you tried using the ‘service’ module instead of ‘shell’?

I really don’t want to do it. I have it like that on many servers.

Is there no way to put it to background?

---

<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:** [July 26, 2012, 7:36pm UTC](https://forum.ansible.com/t/run-daemon-on-a-server/11729/4 "2012-07-26T19:36:26Z")

</div>

Possibly “fire and forget” with async (see docs)

You do have to set a maximum runtime for the command, because Ansible will kill it if not done by then.

---

<div class="post-metadata">

**Author:** ![paweu](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@paweu](https://forum.ansible.com/u/paweu)\
**Post date:** [July 26, 2012, 7:52pm UTC](https://forum.ansible.com/t/run-daemon-on-a-server/11729/5 "2012-07-26T19:52:01Z")

</div>

It works with:

async: 1  
poll: 0

Thanks

W dniu czwartek, 26 lipca 2012 21:36:26 UTC+2 użytkownik Michael DeHaan napisał:

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [July 26, 2012, 9:19pm UTC](https://forum.ansible.com/t/run-daemon-on-a-server/11729/6 "2012-07-26T21:19:05Z")

</div>

> I really don't want to do it. I have it like that on many servers.

Create a template for the init script, push that out through your  
playbook and launch that.

&nbsp;&nbsp;&nbsp;-action: template src=myinit.script.j2 dest=/etc/init.d/$item mode=0755  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- daemon1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- daemon2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- service14  
&nbsp;&nbsp;&nbsp;-action: command /sbin/chkconfig $item on  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- daemon1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- daemon2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- service14  
&nbsp;&nbsp;&nbsp;-action: service name=$item state=running  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- daemon1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- daemon2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- service14

Untested, but you get the idea.

Safest way I'd say.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<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:** [July 27, 2012, 12:40pm UTC](https://forum.ansible.com/t/run-daemon-on-a-server/11729/7 "2012-07-27T12:40:58Z")

</div>

Please, whatever you do next, learn to use pgrep first ! 🙂

What you do can be simplified to:

&nbsp;&nbsp;&nbsp;if ! pgrep daemon &\>/dev/null; then /root/bin/daemon; fi

or you can do:

&nbsp;&nbsp;&nbsp;pgrep daemon &\>/dev/null || /root/bin/daemon

but I prefer the former as it is more explicit.

PS Yes, it is unfortunate that pgrep does not have the -q option.

---

<div class="post-metadata">

**Author:** ![paweu](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@paweu](https://forum.ansible.com/u/paweu)\
**Post date:** [July 27, 2012, 6:28pm UTC](https://forum.ansible.com/t/run-daemon-on-a-server/11729/8 "2012-07-27T18:28:47Z")

</div>

Thanks for the tip 🙂

W dniu piątek, 27 lipca 2012 14:40:58 UTC+2 użytkownik Dag Wieers napisał:
