PID capture of background process launched by Makefile

I have a Makefile that runs a Django web server. I would like the server to start in the background, with the PID being saved in a file.

My recipe is as follows:

run: venv @"${PYTHON}" "${APP}/manage.py" runserver 80 

Intuitively, in order to execute the background process and capture the PID, I would need to do something like this:

 run: venv @"${PYTHON}" "${APP}/manage.py" runserver 80 & ; echo "$$!" > "${LOGDIR}/django.pid" 

This does not work. A sub-shell that uses "make" (/ bin / sh in my case) works when you use:

 <command> & 

... execute the background process and work when using:

 <command> ; <command> 

(or <command> && <command> etc.) for chaining commands. However, when I try to execute the first process and connect the second, I get the following error:

/bin/sh: -c: line 0: syntax error near unexpected token `;'

What is the best way to background process and capture PID in Makefile?

thanks
- B

+6
source share
2 answers

Just leave one ampersand followed by a semicolon:

 run: venv @"${PYTHON}" "${APP}/manage.py" runserver 80 & echo "$$!" > "${LOGDIR}/django.pid" 

Ampersand acts as a command separator just like a semicolon:

 <command> & <command> 
+17
source

If Make gives you too many problems, you can always run the target sh script system; in this script, run the process, background, and then output the pid to the file.

0
source

Source: https://habr.com/ru/post/907722/


All Articles