{"id":7808,"date":"2019-09-02T11:00:54","date_gmt":"2019-09-02T18:00:54","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=7808"},"modified":"2022-04-19T22:09:42","modified_gmt":"2022-04-20T05:09:42","slug":"run-python-script-every-hour","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/run-python-script-every-hour\/","title":{"rendered":"How to Run a Python Script Every Hour, On the Hour"},"content":{"rendered":"<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"\/blog\/images\/schedule-python-script.webp\" title=\"How to Run a Python Script Every Hour\" alt=\"How to Run a Python Script Every Hour\" border=\"0\" width=\"275\" height=\"144\" \/><\/div>\n<p style=\"margin-top:16px;\">\n<a href=\"https:\/\/www.python.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python<\/a> is fast becoming the world&#8217;s most popular coding language. It&#8217;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.\n<\/p>\n<p>One question that frequently comes up on <a href=\"https:\/\/www.reddit.com\/r\/learnpython\/comments\/c2ya1y\/best_way_to_schedule_a_python_script_that_runs\/\" target=\"_blank\" rel=\"noopener noreferrer\">Internet forums<\/a> deals with scheduling:<\/p>\n<div class=\"blog-qa-question-box\">\n<blockquote><p><img decoding=\"async\" src=\"\/images\/quotes-transparent-21x21.png\">&nbsp;&nbsp;How do I schedule my Python script to run every hour on my Windows computer?<\/p><\/blockquote>\n<\/div>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/taskschd\/task-scheduler-start-page\" target=\"_blank\" rel=\"noopener noreferrer\">Windows Task Scheduler<\/a> 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&#8217;s too much busy work.<\/p>\n<p>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&#8217;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 &mdash; for example, if someone accidentally terminated it. Not a very robust approach.<\/p>\n<p>Instead, we recommend using <a href=\"\/products\/AlwaysUp\">AlwaysUp<\/a>. 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&#8217;s how to do that.<\/p>\n<h2 class=\"blog-caption-numbered\">1. Install your Python script as a Windows Service with AlwaysUp<\/h2>\n<p>First, follow our <a href=\"\/products\/AlwaysUp\/Apps\/RunPythonScriptAsAService.html\">step-by-step tutorial<\/a> showing how to run any Python script as a Windows Service with AlwaysUp.<\/p>\n<div align=\"center\"><a href=\"\/products\/AlwaysUp\/Apps\/alwaysup-python-script-running-v7.9.jpg\" class=\"zoomPopup\" title=\"Python running as a Windows Service\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/products\/AlwaysUp\/Apps\/alwaysup-python-script-running-v7.9.jpg\" title=\"Python running as a Windows Service (click to enlarge)\" alt=\"Python running as a Windows Service\" border=\"0\" ><\/a><\/div>\n<p>Your Python script will be configured to run once per day but don&#8217;t worry &mdash; we&#8217;ll adjust that in the next section.<\/p>\n<h2 class=\"blog-caption-numbered\">2. Restart your Python script every hour, on the hour<\/h2>\n<p>Instead of running only once per day, let&#8217;s run your script hourly. To make that change:<\/p>\n<ol type=\"a\">\n<li>\n<p>Edit your Python script in AlwaysUp.<\/p>\n<\/li>\n<li>\n<p>Switch to the <b>Restart<\/b> tab.<\/p>\n<\/li>\n<li>\n<p>Check the <b>Not immediately<\/b> and the <b>On the next hour<\/b> options:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-python-script-hourly.png\" class=\"zoomPopup\" title=\"Restart Python hourly\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-python-script-hourly.png\" title=\"Restart Python hourly (click to enlarge)\" alt=\"Restart Python hourly\" border=\"0\" ><\/a><\/div>\n<\/li>\n<li>\n<p>Click the <b>Save<\/b> button to record your changes.<\/p>\n<\/li>\n<\/ol>\n<h2 class=\"blog-caption-numbered\">3. Minimize logging as your script stops and restarts in the background<\/h2>\n<p>By default, AlwaysUp will record detailed information (in the <a href=\"\/products\/AlwaysUp\/AlwaysUp_EventLog.html\">Windows Event Logs<\/a>) 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.<\/p>\n<p>To reduce the writing to the Event Logs:<\/p>\n<ol type=\"a\">\n<li>\n<p>Edit your Python script in AlwaysUp.<\/p>\n<\/li>\n<li>\n<p>Switch to the <b>Restart<\/b> tab.<\/p>\n<\/li>\n<li>\n<p>Check the <b>Minimize event logging as the application stops &amp; starts<\/b> option:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-python-script-hourly-minimize-logging.png\" class=\"zoomPopup\" title=\"Minimize Python Service Logging\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-python-script-hourly-minimize-logging.png\" title=\"Minimize Python Service Logging (click to enlarge)\" alt=\"Minimize Python Service Logging\" border=\"0\" ><\/a><\/div>\n<\/li>\n<li>\n<p>Click the <b>Save<\/b> button to record your changes.<\/p>\n<\/li>\n<\/ol>\n<p style=\"margin-top:30px\">\nAnd that&#8217;s it. From now on, your Python script will run predictably &mdash; every hour, on the hour.\n<\/p>\n<p>Please be sure to <a href=\"\/support\">get in touch<\/a> if you have any questions about running your script as a service (or anything else).<\/p>\n<!-- relpost-thumb-wrapper --><div class=\"relpost-thumb-wrapper\"><!-- filter-class --><div class=\"relpost-thumb-container\"><style>.relpost-block-single-image, .relpost-post-image { margin-bottom: 10px; }<\/style><h3>You may also like...<\/h3><div style=\"clear: both\"><\/div><div style=\"clear: both\"><\/div><!-- relpost-block-container --><div class=\"relpost-block-container relpost-block-column-layout\" style=\"--relposth-columns: 3;--relposth-columns_t: 2; --relposth-columns_m: 2\"><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/vb6-memory-leaks\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: How do I Make AlwaysUp Babysit my Leaky VB6 Program?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/memory-leaks-growing-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: How do I Make AlwaysUp Babysit my &quot;Leaky&quot; VB6 Program?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/run-multiple-windows-services\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"6 Reasons Why AlwaysUp is the Best Tool to Run Multiple Applications as Windows Services\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/running-multiple-windows-services-150x150-1.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">6 Reasons Why AlwaysUp is the Best Tool to Run Multiple Applications as Windows Services<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/version-10-5\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"New Version\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/new-version-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">AlwaysUp 10.5: Restricted Rights, Smoother Shutdowns, and more<\/h2><\/div><\/div><\/a><\/div><!-- close relpost-block-container --><div style=\"clear: both\"><\/div><\/div><!-- close filter class --><\/div><!-- close relpost-thumb-wrapper -->","protected":false},"excerpt":{"rendered":"<p>Python is fast becoming the world&#8217;s most popular coding language. It&#8217;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 &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/run-python-script-every-hour\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":7983,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[26,125,126,140],"class_list":["post-7808","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-alwaysup","tag-alwaysup-tag","tag-python","tag-python-script","tag-schedule"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/7808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=7808"}],"version-history":[{"count":2,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/7808\/revisions"}],"predecessor-version":[{"id":11245,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/7808\/revisions\/11245"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/7983"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=7808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=7808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=7808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}