Check if a Python script is already running in Windows
NickName:shlomiLan Ask DateTime:2017-01-03T06:04:30

Check if a Python script is already running in Windows

What is the best/easiest why to check if a specific Python script already running in Windows?

I have a script that goes over all files in a folder and copies them to another folder (sort to Movie or TV Shows folder). I want to make sure when the script starts that there isn't another process (of the same script) that is already running, so I wouldn't have issues with 2 scripts that are trying to move the same files.

I have tried to create a file in the start of the script and deleting it when the script finishes, but I got into problems when the script crashes and/or throws an error.

I know that I can use psutil, but then I will get the process name (python.exe) and I'm looking for a why to distinguish if the Python process is running my script or another program.

Copyright Notice:Content Author:「shlomiLan」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/41433819/check-if-a-python-script-is-already-running-in-windows

Answers
Roland Smith 2017-01-02T22:26:08

You can use psutil.Process().cmdline() to see the complete command line of a process.\n\nAlternatively, you could lock the files you're working on. See the answer to this question how to do this on ms-windows. The thing with locks is that you have to be careful to remove them, especially when an error occurs.",


user11394093 2019-04-22T09:25:08

For Windows o.s. you can use timestamp.txt file\n\ntimestamp = 'timestamp.txt'\n...\nelif windows:\n try:\n new_timestamp = False\n if not os.path.exists(timestamp):\n new_timestamp = True\n try:\n with open(timestamp, 'a') as f_timestamp:\n f_timestamp.write(str(int_t0))\n except IOError as e:\n out1 = 'M. Cannot open file for writing. Error: %s - %s.' \\\n % (e.logfile, e.strerror) + ' -> Exit code 3'\n logging.error(out1)\n sys.exit(3)\n\n if not new_timestamp and os.path.exists(timestamp):\n out1 = 'N. Script ' + __file__ + ' is already running.'\n print(out1)\n logging.error(out1)\n sys.exit(0)\n\n except IOError as e:\n out1 = 'J. Cannot open file. Error: %s - %s.' \\\n % (e.filepath, e.strerror) + ' -> Exit code 4'\n logging.error(out1)\n\n...\ntry:\n f_timestamp.close()\n os.remove(timestamp)\nexcept OSError as e:\n logging.error('B. Cannot delete ' + timestamp + \\\n ' Error: %s - %s.' % (e.filename, e.strerror))\n",


Marat 2017-01-02T22:30:18

Use lockfile. It is cross-platform, uses native OS functions and much more robust than any home-brewn lock file creation schemas",


More about “Check if a Python script is already running in Windows” related questions

Check if a Python script is already running in Windows

What is the best/easiest why to check if a specific Python script already running in Windows? I have a script that goes over all files in a folder and copies them to another folder (sort to Movie ...

Show Detail

How to check if a script is already running

How do you check if your script is already running to prevent it from running/opening multiple times. I am aiming for it to either quit and allow the script to open again or stop the new one from r...

Show Detail

Check if Python Script is already running

i would like my Python script to check, if it is already running and if yes, kill the old process. i tried this, but it doesn't really work... import os import subprocess import signal process_na...

Show Detail

Check if a particular Python script is already running

How can I check, from a Python script, if another specific Python script is running? def running(): for q in psutil.process_iter(): print q if q.name() == 'server_class.py': ...

Show Detail

How to check if Python script is already running

I have python script on Ubuntu, which sometimes running more than 24 hours. I have set in cron, to run this script every day. However if script is still running, I would like to terminate new insta...

Show Detail

How to check a python script (test.py)is running or not on windows 10

I'm trying to setup a socket connection between Python and PHP. Python will function as server and PHP as client. I have a python script (test.py) running form PHP. `python test.py` how to check...

Show Detail

Checking if script is already running (python / linux)

I am trying to add a function to a script to check if it is already running. This is because the script will be started with a cronjob. Here a stub of what i attempted for that function: import p...

Show Detail

Lotus Script - check if windows process is running

I'm new to programming. I've made a simple codeblock that runs a Windows program POSTOPEN when I open an application in Lotus Notes 8.5.3 (basic) ' [ML]Check if user is member of the [ConnectClient]

Show Detail

PHP Verify already running script on WIndows

I am creating a script that will run every hour using Windows Task Scheduler. But I don't want the script to launch if it's already running. Is it possible to do this with Windows Task Scheduler? I...

Show Detail

check if application is already running or restart

check if application is already running or restart We can check and restart if a application service is running within SQL. This check can also be done with a SQL JOB. The application is written i...

Show Detail