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.
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.
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.
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.
I think you mean "cross-platform single instance of application written in python". Try this workable solution: Python: single instance of program
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With