Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to constantly run Python script in the background on Windows?

Tags:

I have created a script that moves files from one folder to another. But since the original folder is the Downloads folder I need it to always run in the background.

I also have a standard Batch file that looks something like this:

@py C:\\Python\Scripts\moveDLs.py %*

I'm using Windows 10. I have found info for Linux and OS on how to use nohup in the batch file. Is there a Windows version?

If there is do you need to execute the script every time you restart or switch the PC on?

Also, how do you terminate the process when you do manage to make it permanent?

Many Thanks

like image 216
Thanos Dodd Avatar asked Dec 01 '19 12:12

Thanos Dodd


People also ask

How do I run a Python program in the background in Windows?

The easiest way of running a python script to run in the background is to use cronjob feature (in macOS and Linux). In windows, we can use Windows Task Scheduler. You can then give the path of your python script file to run at a specific time by giving the time particulars.

How do I keep a Python program running forever?

Challenge: Run a piece of Python code forever—until it is forcefully interrupted by the user. Solution: use a while loop with a Boolean expression that always evaluates to True . Examples: have a look at the following variants of an infinite while loop.

How do I run a Python script in Windows daily?

In order to schedule the Python script using the Windows Scheduler: Open the Windows Control Panel and then click on the Administrative Tools. Double-click on the Task Scheduler, and then choose the option to 'Create Basic Task…'


2 Answers

On Windows, you can use pythonw.exe in order to run a python script as a background process:

Python scripts (files with the extension .py) will be executed by python.exe by default. This executable opens a terminal, which stays open even if the program uses a GUI. If you do not want this to happen, use the extension .pyw which will cause the script to be executed by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on startup.

For example,

C:\ThanosDodd\Python3.6\pythonw.exe C:\\Python\Scripts\moveDLs.py

In order to make your script run continuously, you can use sched for event scheduling:

The sched module defines a class which implements a general purpose event scheduler

import sched
import time

event_schedule = sched.scheduler(time.time, time.sleep)

def do_something():
    print("Hello, World!")
    event_schedule.enter(30, 1, do_something, (sc,))

event_schedule.enter(30, 1, do_something, (s,))
event_schedule.run()

Now in order to kill a background process on Windows, you simply need to run:

taskkill /pid processId /f

Where processId is the ID of the process you want to kill.

like image 165
Giorgos Myrianthous Avatar answered Sep 20 '22 12:09

Giorgos Myrianthous


One option is to change your script so it is intended to run continuously rather than repeatedly. Just wrap the whole thing in a while loop and add a sleep.

import time

while True:
   your_script_here
   time.sleep(300)

In order to make sure this starts up with the machine and to provide automatic restarts in the event of an exception I'd recommend making it into a Windows service using Non-Sucking Service Manager (www.nssm.cc). There are a few steps to this (see the docs) but once done your script will be just another windows service which you can start and stop from the standard services.msc utility.

like image 44
SimonN Avatar answered Sep 23 '22 12:09

SimonN