Run daemon on a server

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.

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

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?

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.

It works with:

async: 1
poll: 0

Thanks

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

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

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

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.

Thanks for the tip :slight_smile:

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