After writing your script, you might want to schedule it to run periodically, let’s say, Daily, Weekly or Any Time interval you will decide

Before you schedule that you want to  simply create a batch file and schedule it run with a Windows Scheduler.

Here are simple steps to do that.

  1. First of all, if you want your script to run with a particular Python version you can decide that by declaring shebang line. For instance , if you want your script to run with Python 2.7 , you shebang line will be #! python2.7 shebang line
  2. This will ensure that your script runs without breaking due to any different Python version
  3. To create a batch file, simply open a new Notepad and
  4. Type the followingecho C:\Users\adaba\Anaconda3\envs\gl-env\python.exe C:\TREES\customer_segmentation_maoting\buyer_behaviour_analysis.py %* PAUSE
  5. The echo will print out what is being executed, in this case our python script file being called and the results
  6. The first file path is the file path to the Python executable which want to use to run the Python script . (In my case , it is Python 2.7 as I have different versions installed)
  7. The second file path is the path to the Python script we want to run which in this case is buyer_behaviour_analysis.py
  8. The PAUSE will hold the command prompt open even after the file finishes executing and you can press any key to exit the command prompt window afterwards
  9. The echo and PAUSE are optional. You can ignore the PAUSE and the command prompt will close automatically once the script finishes running.
  10. Now save the file with .bat extension. eg runscript.batbatch file for python notepad
  11. You can test the batch file to see if it works by double click on it.
  12. Now that it works fine. You can schedule Window’s task for it to run periodically
  13. To do this , go to windows Scheduler by typing scheduler in the start menu search boxwindows scheduler
  14. Create a basic task windows scheduler create basic task
  15. Give it a name and descriptionscheduler name and description
  16. Select the PERIOD you want it run, whether, DAILY, WEEKLY, ONE TIME, etc
  17. On the NEXT TAB Set the DATE and TIME
  18. On the NEXT TAB , what action you want to do, choose Start a Program scheduler action you want to do
  19. On the NEXT TAB , BROWSE to the BATCH FILE you created; runscript.bat  and select that.
  20. Make sure to tick the “Open the Properties dialog for this task when I click Finish” on the next tab and click Finish. (The open the properties dialog will allow you to do some further configurations. )open the property dialogue windows scheduler
  21. One configuration you would want to choose is to click on the CHOOSE USER or GROUP button in addition to the other ticks as shown in the image belowchoose user or group windows scheduler
  22. In the popup window from the CHOOSE USER or GROUP, type the user as SYSTEM. This will allow the script to be run by system and it will not output any code or results to the user’s console. It will basically run the whole script in the background till completion. (It will therefore be ideal to have a logging system in your script to track some events. There is a simple note on python logging here)user system windows scheduler
  23. You can continue and do some other settings as you would need by using the tabs above the scheduler other tab settings windows scheduler
  24. After all settings are done, CLICK OK and your Python script is scheduled to run at the time you indicated. Keep track for the few initial runs to make sure everything is fine and you can leave it to run on its own thereafter.
  25. Happy batching and scheduling!

Similar Posts

Leave a Reply

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