how to make a python file run in the background code example

Example 1: how to run a python script in background windows

> pythonw script.py

Example 2: how to check any script is running in background linux using python

def check_process():
    import subprocess
    script_name = "test.py"
    cmd='pgrep -f .*python.*{}'.format(script_name)
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    my_pid, err = process.communicate()
    if len(my_pid.splitlines()) >0:
        print("Script Running in background")
        sys.exit(0);
check_process()

Example 3: how to make a python file run in the background

#Cmd
C:\..\>cd DIRECTORY
C:\..\DIRECTORY>pythonw python.py
#Close in Task Manager

Tags:

Misc Example