Run daemon on a server

727 views
Skip to first unread message

paweu

unread,
Jul 26, 2012, 3:24:07 PM7/26/12
to ansible...@googlegroups.com
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.

Mark Theunissen

unread,
Jul 26, 2012, 3:26:02 PM7/26/12
to ansible...@googlegroups.com
Could you make an init script for it? Have you tried using the 'service' module instead of 'shell'?

service name=mydaemon state=started

paweu

unread,
Jul 26, 2012, 3:31:12 PM7/26/12
to ansible...@googlegroups.com

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?

Michael DeHaan

unread,
Jul 26, 2012, 3:36:26 PM7/26/12
to ansible...@googlegroups.com
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.

paweu

unread,
Jul 26, 2012, 3:52:01 PM7/26/12
to ansible...@googlegroups.com
It works with:

    async: 1
    poll: 0

Thanks

Jan-Piet Mens

unread,
Jul 26, 2012, 5:19:05 PM7/26/12
to ansible...@googlegroups.com
> 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.

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

Untested, but you get the idea.

Safest way I'd say.

-JP

Dag Wieers

unread,
Jul 27, 2012, 8:40:58 AM7/27/12
to ansible...@googlegroups.com
On Thu, 26 Jul 2012, paweu wrote:

> 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

Please, whatever you do next, learn to use pgrep first ! :-)

What you do can be simplified to:

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

or you can do:

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.

--
-- dag wieers, d...@wieers.com, http://dag.wieers.com/
-- dagit linux solutions, in...@dagit.net, http://dagit.net/

[Any errors in spelling, tact or fact are transmission errors]

paweu

unread,
Jul 27, 2012, 2:28:47 PM7/27/12
to ansible...@googlegroups.com
Thanks for the tip  :)
Reply all
Reply to author
Forward
0 new messages