How to know which is the PID of a currently running Python script?

Asked

Viewed 940 times

2

I have a certain Python script that I want to know what is the current PID of it, through the script itself.

How can I do that?

Through this too, I can determine that it be executed only once?

  • You want the script itself to return your PID or the script to return the PID of another script?

  • "Currently running". the script itself, I will edit :p

1 answer

4

Before we answer, let’s talk a little about PID.

Process Identifier (process identifier), PID, or process ID is a unique number that is assigned by the operating system when a process is executed. This number is used to refer to a process that is running.

source: wikipedia

In short, each process run on the operating system will have its own PID.

There are some ways to do this, but the simplest is just to import the os and use the method getpid() to obtain the PID of the current process.

Example:

import os

pid = os.getpid()

You can also get the PID based on the name of the case, and other forms.

See too many shapes in this question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.