The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation


How to Run a Python Script Every Hour, On the Hour

How to Run a Python Script Every Hour

Python is fast becoming the world’s most popular coding language. It’s no surprise that more and more administrators are turning to the simple, efficient, and ubiquitous platform for a variety of day-to-day tasks.

One question that frequently comes up on Internet forums deals with scheduling:

  How do I schedule my Python script to run every hour on my Windows computer?

The Windows Task Scheduler seems like it should do the job, but unfortunately it falls short because it has no ability to restart an application at a fixed time. You would have to set up 24 tasks, one for each hour of the day. That’s too much busy work.

You could also create a batch file that runs the script in an infinite loop and launch the batch file with Task Scheduler. That would be easier, but the lack of a fixed schedule means that you wouldn’t know the time when the script would be run. And with zero monitoring and error reporting, you would be left in the dark if the script somehow stopped running — for example, if someone accidentally terminated it. Not a very robust approach.

Instead, we recommend using AlwaysUp. Simply setup your Python script to run as a background Windows Service, then configure AlwaysUp to launch your script each hour on the hour. Here’s how to do that.

1. Install your Python script as a Windows Service with AlwaysUp

First, follow our step-by-step tutorial showing how to run any Python script as a Windows Service with AlwaysUp.

Python running as a Windows Service

Your Python script will be configured to run once per day but don’t worry — we’ll adjust that in the next section.

2. Restart your Python script every hour, on the hour

Instead of running only once per day, let’s run your script hourly. To make that change:

  1. Edit your Python script in AlwaysUp.

  2. Switch to the Restart tab.

  3. Check the Not immediately and the On the next hour options:

    Restart Python hourly
  4. Click the Save button to record your changes.

3. Minimize logging as your script stops and restarts in the background

By default, AlwaysUp will record detailed information (in the Windows Event Logs) whenever the application it is monitoring starts and stops. This is fine for programs designed to operate 24/7, but that logging can be overwhelming for a script that starts and stops frequently.

To reduce the writing to the Event Logs:

  1. Edit your Python script in AlwaysUp.

  2. Switch to the Restart tab.

  3. Check the Minimize event logging as the application stops & starts option:

    Minimize Python Service Logging
  4. Click the Save button to record your changes.

And that’s it. From now on, your Python script will run predictably — every hour, on the hour.

Please be sure to get in touch if you have any questions about running your script as a service (or anything else).

Posted in AlwaysUp | Tagged , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *