Saving the PID of a spawned process in a Makefile

I currently have a Makefile rule:

start: ./start.sh 

which runs a very simple server, needed as part of the build process. I have another rule to stop the server:

 stop: kill `cat bin/server.PID` 

here is the start.sh script:

 #!/bin/bash cd bin python server.py & echo $! > server.PID 

NB server.py should be launched from the bin directory

I would like to implement the start.sh functionality at the beginning of the rule, I tried a lot of things, but I can not get the PID.

+8
makefile pid
source share
1 answer

I do not understand where you are stuck. What is wrong with

 start: cd bin && { python server.py & echo $$! > server.PID; } 

?

You can also make the pidfile target and dependency:

 start: server.PID server.PID: cd bin && { python server.py & echo $$! > $@; } stop: server.PID kill `cat $<` && rm $< .PHONY: start stop 
+19
source share

All Articles