问题描述
我有一个启动Django Web服务器的Makefile.我希望服务器在后台启动,并将PID保存到文件中.
I have a Makefile that starts a Django web server. I would like the server to be started in the background, with the PID saved to a file.
我的食谱如下:
run: venv
@"${PYTHON}" "${APP}/manage.py" runserver 80
直观地讲,要使该过程成为背景并捕获PID,我必须执行以下操作:
Intuitively, to background the process and capture the PID, I'd have to do something like this:
run: venv
@"${PYTHON}" "${APP}/manage.py" runserver 80 & ; echo "$$!" > "${LOGDIR}/django.pid"
这是行不通的.当您使用以下命令时,"make"使用的子外壳(在我的情况下为/bin/sh)有效:
This doesn't work, though. The sub-shell that 'make' uses (/bin/sh in my case) works when you use:
<command> &
...为流程提供背景,并在使用时起作用:
...to background a process, and works when use:
<command> ; <command>
(或<command> && <command>
等)来链接命令.但是,当我尝试后台处理第一个进程并链接第二个进程时,出现以下错误:
(or <command> && <command>
, etc...) to chain commands. However, when I try to background the first process and chain a second one, I get the following error:
/bin/sh: -c: line 0: syntax error near unexpected token `;'
使进程后台并在Makefile中捕获PID的最佳方法是什么?
What is the best way to background a process and capture the PID in a Makefile?
谢谢
-B
推荐答案
只需留下一个&"号,后面不要使用分号:
Just leave a single ampersand, without a semicolon after it:
run: venv
@"${PYTHON}" "${APP}/manage.py" runserver 80 & echo "$$!" > "${LOGDIR}/django.pid"
Ampersand用作命令分隔符,与分号相同:
Ampersand acts as command separator in the same way as a semicolon:
<command> & <command>
这篇关于捕获由Makefile启动的后台进程的PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!