joidegn
joidegn

Reputation: 1068

Daemonize process in makefile

Im struggling to get a process started in a Makefile to be launched in the background and the Makefile target to complete.

It's a simple flask and dramatiq application. This is the target:

.PHONY: run-daemon                                                                                                                                                                                                                                             
run-daemon:                                                                                                                                                                                                                                                    
    poetry run gunicorn wsgi:app --daemon --workers 16 --threads 16                                                                                                                                                                                            
    poetry run dramatiq api:broker worker.tasks --processes 1

First I tried using ampersand to background the processes:

.PHONY: run-daemon                                                                                                                                                                                                                                             
run-daemon:                                                                                                                                                                                                                                                    
    poetry run gunicorn wsgi:app --daemon --workers 16 --threads 16 &                                                                                                                                                                                            
    poetry run dramatiq api:broker worker.tasks --processes 1 &

With that gunicorn backgrounds as expected but dramatiq does not.

Then I tried nohup and setsid in various combinations and with and without ampersand but neither of these result in backgrounding dramatiq.

.PHONY: run-daemon                                                                                                                                                                                                                                             
run-daemon:                                                                                                                                                                                                                                                    
    poetry run gunicorn wsgi:app --daemon --workers 16 --threads 16 &                                                                                                                                                                                            
    setsid nohup poetry run dramatiq api:broker worker.tasks --processes 1 &

As it is it does not complete because the dramatiq process does not complete.

But running dramatiq directly like so poetry run dramatiq api:broker worker.tasks --processes 1 results in dramatiq running in the background.

How can I daemonize it such that the make target completes?

Upvotes: 0

Views: 518

Answers (1)

bobah
bobah

Reputation: 18864

You need to use backslash to create multiline combos executed as a single shell command.

I would say this should do (pay attention to escaping $ and Co.):

SHELL:=/bin/bash
.PHONY run-daemon
run-daemon:
        daemon-executable > daemon.log 2>&1&\
        disown -h $$!

Upvotes: 1

Related Questions