How to get pid of my make command in the Makefile?

make starts a new shell for each command it starts. Using bash (didn't check for other shells) echo $PPID gives the parent process ID (which is make).

all: subtarget 
        echo $$PPID
        echo $(shell echo $$PPID)

subtarget:
        echo $$PPID

Here is the answer to your question, which nobody seemed to want to give you:

TEMPDIR := /tmp/myprog.$(shell ps -o ppid $$$$)

Of course, this is not guaranteed to be unique.


Try mktemp for creating unique temporary filenames. The -d option will create a directory instead of a file.

TEMPFILE := $(shell mktemp)
TEMPDIR := $(shell mktemp -d)

Note the colon. (Editor's note: This causes make to evaluate the function once and assign its value instead of re-evaluating the expression for every reference to $(TEMPDIR).)


TEMPDIR := $(shell mktemp)

The problem is: every time you run make, it will create a temporary file. Regardless of you use it or not and regardless of the make target you use. That means: either you delete this file in every target or you they will not be deleted anytime.

I prefer to add the -u parameter:

TEMPDIR := $(shell mktemp -u)

This makes mktemp to create an unique filename without creating the file. Now you can create the file in the targets you need them. Of course there is a chance of a race conditions, where the file is used by another process before you create it. That is very unlikely, but do not use it in an elevated command, like make install.