Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to use a temp directory that will be unique to this build. How can I get the pid of my make command in the Makefile?

I tried:

TEMPDIR = /tmp/myprog.$$$$

but this seems to store TEMPDIR as /tmp/myprog.$$ and then eval as a new pid for every command which refs this! How do I get one pid for all of them (I'd prefer the make pid, but anything unique will do).

Thanks in advance.

like image 594
sligocki Avatar asked Oct 05 '10 00:10

sligocki


People also ask

How do you find the PID of just started?

Compile the little C program here into a binary called start (or whatever you want), then run your program as ./start your-program-here arg0 arg1 arg2 ... Long story short, this will print the PID to stdout , then load your program into the process. It should still have the same PID.

What is the command to get the PID of the most recent background command process?

Get PID : #!/bin/bash my-app & echo $! Save PID in variable: #!/bin/bash my-app & export APP_PID=$!

What is $$ in Makefile?

$$ means be interpreted as a $ by the shell. the $(UNZIP_PATH) gets expanded by make before being interpreted by the shell.


2 Answers

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).)

like image 111
Matthew Iselin Avatar answered Oct 07 '22 14:10

Matthew Iselin


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
like image 25
Konrad Avatar answered Oct 07 '22 14:10

Konrad