Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving PID of spawned process within a Makefile

Tags:

makefile

pid

I currently have a Makefile rule thus:

start:
    ./start.sh

which starts a very simple server needed as part of the build process. I have another rule for stopping 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 must be run from within the bin directory

I'd like to implement the functionality of start.sh within the start rule, I've tried numerous things but can't seem to get the PID.

like image 349
Chris Camacho Avatar asked Apr 29 '14 13:04

Chris Camacho


1 Answers

I don't understand where you're getting stuck. What's wrong with

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

?

You can also make the pidfile a target and dependency:

start: server.PID

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

stop: server.PID
    kill `cat $<` && rm $<

.PHONY: start stop
like image 168
reinierpost Avatar answered Nov 03 '22 22:11

reinierpost