在脚本本身内获取shell/python脚本的pid

Akh*_*ran 3 python-2.7

我需要在脚本本身内部获取shell脚本的pid,并且需要将其传递给另一个脚本.

如何在脚本本身中获取特定脚本的pid?

use*_*631 7

可以检索当前进程的进程ID os.getpid()(当然也存在其他更复杂的方法).

import os

print "This process has the PID", os.getpid()
Run Code Online (Sandbox Code Playgroud)

在bash中,您可以使用$$获取运行脚本的bash实例的PID.