如何在 Makefile 中获取 make 命令的 pid?
我想使用此构建特有的临时目录。如何在 Makefile 中获取 make 命令的 pid?
我尝试过:
TEMPDIR = /tmp/myprog.$$$$
但这似乎将 TEMPDIR
存储为 /tmp/myprog.$$
,然后将 eval 作为引用此的每个命令的新 pid !我如何获得所有这些的一个pid(我更喜欢make pid,但任何独特的东西都可以)。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试使用 mktemp 创建唯一的临时文件名。
-d
选项将创建一个目录而不是文件。注意冒号。 (编者注:这会导致 make 对函数求值一次并为其赋值,而不是为每个对 $(TEMPDIR) 的引用重新求表达式。)
Try mktemp for creating unique temporary filenames. The
-d
option will create a directory instead of a file.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).)
make
为它启动的每个命令启动一个新的 shell。使用 bash(未检查其他 shell)echo $PPID
给出父进程 ID(即 make)。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).问题是:每次运行 make 时,它都会创建一个临时文件。无论您是否使用它,也无论您使用的 make 目标如何。这意味着:要么您在每个目标中删除该文件,要么它们随时都不会被删除。
我更喜欢添加 -u 参数:
这使得 mktemp 创建唯一的文件名而不创建文件。现在您可以在需要的目标中创建文件。当然,有可能出现竞争条件,即文件在创建之前已被另一个进程使用。这不太可能,但是不要在提升的命令中使用它,例如
make install
。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:
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
.这是您问题的答案,似乎没有人愿意给您:
当然,这不能保证是唯一的。
Here is the answer to your question, which nobody seemed to want to give you:
Of course, this is not guaranteed to be unique.
您可以使用日期字符串。除非您同时启动多个构建,否则这应该非常接近。
类似于以下
更新:如评论中所述,如果您使用 name = val 语法设置变量,则它是 每次使用时都会重新评估。 := 语法设置它并且不重新评估该值,尽管使用反引号 `` 似乎可以以某种方式解决这个问题。您确实想使用 $(shell CMD) 构造来完成这样的事情。
You could use a date string. Unless you're kicking off multiple builds at the same time this should be pretty close.
Something like the following
Update: As noted in the comment if you set a variable with name = val syntax it is re-evaluated each time it's used. The := syntax sets it and doesn't re-evaluate the value, though using back-ticks `` seems to get around this somehow. You really want to use the $(shell CMD) construct for stuff like this.
另外,你可以像这样得到你的 make pid。
PID := $(shell ps | tail -n 6 | head -n 1 | cut -f1 -d' ')
Also, you can get your make pid like this.
PID := $(shell ps | tail -n 6 | head -n 1 | cut -f1 -d' ')