Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if current process is running using Python?

Tags:

python

process

I need to add a function to my python script that checks if the current script is already running. If it is then it will quit, if not it continues running the script. I've looked into methods of doing this but I cant figure out how to do it.

like image 633
AustinM Avatar asked Feb 19 '11 02:02

AustinM


People also ask

How do I get PID in Python?

We can get the pid for the current process via the os. getpid() function. We may also get the pid for the parent process via the os. getppid() function.

How do I know if a python process is running Linux?

on linux, you can look in the directory /proc/$PID to get information about that process. In fact, if the directory exists, the process is running. Save this answer.

How do I list running processes in Windows using Python?

We would be using the WMI. Win32_Process function in order to get the list of running processes on the system. Then we called the function WMI. Win32_Process() to get the running processes, iterated through each process and stored in variable process.


2 Answers

I think you mean "cross-platform single instance of application written in python". Try this workable solution: Python: single instance of program

like image 188
Drake Guan Avatar answered Oct 02 '22 12:10

Drake Guan


You need to communicate using a shared resource. In the simplest sense, you use the resource as a mutex. Lock or pid files are commonly used this way on *nix by using the filesystem as that shared resource.

Depending on need, you can use a shared resource that allows communication; e.g. a web browser executed to open a given URL will communicate the URL to an existing process, if there is one.

The type of shared resources available is platform-specific.

like image 20
Fred Nurk Avatar answered Oct 02 '22 11:10

Fred Nurk