5

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.

jww
  • 97,681
  • 90
  • 411
  • 885
AustinM
  • 773
  • 6
  • 18
  • 27
  • 1
    Have you tried to just execute system commands... if on linux ps aux | grep python ? see this link: http://stackoverflow.com/questions/38056/how-do-you-check-in-linux-with-python-if-a-process-is-still-running – Nix Feb 19 '11 at 02:54
  • Well I need it to work on Windows too, not just Linux – AustinM Feb 19 '11 at 02:59
  • 2
    Perhaps you are looking for the psutil module: http://code.google.com/p/psutil/ – unutbu Feb 19 '11 at 03:07
  • [Check if a process is running using Python on Linux](https://stackoverflow.com/q/7647167/608639), [How to check if a process is still running using Python on Linux?](https://stackoverflow.com/q/38056/608639), [Python check if a process is running or not](https://stackoverflow.com/q/7787120/608639), etc. – jww Sep 19 '19 at 07:14

2 Answers2

9

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

Community
  • 1
  • 1
Drake Guan
  • 14,514
  • 15
  • 67
  • 94
1

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.

Fred Nurk
  • 13,952
  • 4
  • 37
  • 63