Daemonizing an executable in ansible
Asked Answered
P

5

23

I am trying to create a task in ansible which executes a shell command to run an executable in daemon mode using &. Something like following

-name: Start daemon
  shell: myexeprogram arg1 arg2 &

What am seeing is if I keep & the task returns immediately and the process is not started . If I remove & ansible task waits for quite some time without returning.

Appreciate suggestion on proper way to start program in daemon mode through ansible. Pls note that I dont want to run this as a service but an adhoc background process based on certain conditions.

Pone answered 22/4, 2015 at 19:4 Comment(0)
L
18

Running program with '&' does not make program a daemon, it just runs in background. To make a "true daemon" your program should do steps described here.

If your program is written in C, you can call daemon() function, which will do it for you. Then you can start your program even without '&' at the end and it will be running as a daemon.

The other option is to call your program using daemon, which should do the job as well.

- name: Start daemon
  shell: daemon -- myexeprogram arg1 arg2
Leahleahey answered 23/4, 2015 at 11:54 Comment(3)
Please note that the linked steps are considered an anti-pattern nowadays: For developing a new-style daemon, none of the initialization steps recommended for SysV daemons need to be implemented. New-style init systems such as systemd make all of them redundant. Moreover, since some of these steps interfere with process monitoring, file descriptor passing and other functionality of the init system, it is recommended not to execute them when run as new-style service.Excrement
What is the -- for ?Malvaceous
The --, by POSIX, is a separator between command's options, which all begin with a dash -, and arguments, which may also contain dashes and could thus confuse the options-parsing... For example, to list a file safely, you should do ls -- $filename -- otherwise your command may fail, if the name of the file itself begins with a dash.Sulfuric
H
13

When you (or Ansible) log out the exit signal will still be sent to the running process, even though it is running in the background.

You can use nohup to circumvent that.

- name: Start daemon
  shell: nohup myexeprogram arg1 arg2 &

http://en.wikipedia.org/wiki/Nohup

Heyward answered 23/4, 2015 at 8:44 Comment(3)
This is run inside of an non interactive SSH session so this process will die as soon as the session is.Badr
Other people report being able to get this to work... superuser.com/a/870925/57697 but having said that, I cannot replicate their success.Hit
guys then what is the other way of achieving this. I am not able to get this running using daemon as well as nohup..The ansible script still waits..and if i terminate the ansible script the process on remote host also terminatesStilton
E
5

From the brief description on what you want to achieve, it sounds like it would be best for you to set up your executable as a service (using Upstart or similar) and then start/stop it as needed based on the other conditions that require it to be running (or not running).

Trying to run this as a process otherwise will entail having to capture the PID or similar so you can try and shut down the daemon you have started when you need to, with pretty much the same amount of complexity as installing an init config file would take and without the niceties that systems such as Upstart give you with the controls such as start/stop.

Esquimau answered 23/4, 2015 at 16:46 Comment(0)
G
2

I found the best way, particularly because I wanted output to be logged, was to use the "daemonize" package. If you are on CentOS/Redhat, like below. There is probably also an apt-package for it.

- name: yum install daemonize
  yum:
    name: daemonize
    state: latest

- name: run in background, log errors and standout to file
  shell:  daemonize -e /var/log/myprocess.log -o /var/log/myprocess.log  /opt/myscripts/myprocess.sh
Girdler answered 20/11, 2017 at 20:21 Comment(0)
C
0

Adding to the daemonize suggestions above, if you want to start your program in a specific directory you can do:

- name: install daemonize package
  package:
    name: daemonize
    state: latest
- name: start program
  command: daemonize -c /folder/to/run/in /path/to/myexeprogram arg1 arg2

Notably, you also probably want the -e -o flags to log output.

Carousel answered 26/3, 2019 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.