Run Python API script as scheduled task?

9660
35
Jump to solution
04-18-2018 01:38 PM
ShawnHibbs1
New Contributor II

I have created a script that updates a feature service through the Python API in Jupyter Notebooks. I was wondering how I can run this as a scheduled task? 

35 Replies
PhilipFrancis
New Contributor II

Equally important to digest alongside the actual question is the following: whether it can be done and how is 100% system-dependent and context-dependent. As an example of context, environment settings can be different at a command prompt (i.e., via manual entry at CLI) from settings in a scheduled task (especially if one is not necessarily 'logged in' when the task occurs). As classic example of this is env differences on Linux systems when run under someone's crontab vs when run manually at a terminal.

I know this is off-topic, but I do this professionally and teach it at a university, and ~90% of folks simply don't remember this small fact which, eventually can inhibit the process of tracking down the cause of a problem when the 'correct answer' is not working.

0 Kudos
PhilipFrancis
New Contributor II

Hi Shawn, where does the feature reside, and is this on the same system as the notebook?

0 Kudos
Roy__Allen_Gilbert
New Contributor

I've had great success using PyInstaller to compile the .py script and all its necessary components into a single executable that I can then run in Windows Task Scheduler. This frees you from worrying how to call a particular virtual environment. This also allows you to share the executable with different systems without setting up that same Python environment.

I have found a significant resulting .exe size if the API was installed with or without dependencies. If you install the arcgis package using the default command (e.g. conda install -c esri arcgis or pip install arcgis) the resulting .exe file size can be > 200 MB. Installing without dependencies (e.g. conda install -c esri arcgis --no-deps or pip install arcgis --no-deps) the resulting .exe file size can be < 40 MB. It may not be possible to install without dependencies in your case but I would recommend doing so if you can.

PyInstaller Compiling Snippet:

import subprocess

env = <Enter the path to your virtual environment.>
fn = <Enter the desired output file name.>
py = <Enter the path to your .py script.>
subprocess.check_call(f"{env}/Scripts/pyinstaller -n {fn} -F {py}")

DavidAskov1
Occasional Contributor

I don't know about running a Jupyter notebook. I would recommend making it a .py script and run it at your command line. Assuming this is Windows (since you said "scheduled task" and not "cron"), make sure you can call it from the DOS prompt. I recommend calling it as "<python executable> <script> <parameters>". 

Get it working that way, and then you should be able to schedule it no problem. In the "actions" tab of a Windows scheduled task, I specify the full path to my Python executable for the "Program/script" textbox, e.g.: 

"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe"

Note that this is in double-quotes because it contains a space. Then, in the "Add arguments (optional)" text box, I specify the full path to the Python script, again putting it in double quotes if it contains a space in it. 

On the "General" tab, I've found that checking "Run with highest privileges" is necessary in some cases. Full disclosure - I don't really understand that, but it resolved some issues I had once, so I check it. 

0 Kudos
AndrewHargreaves1
New Contributor III

Hello Shawn

Could you provide an example of your script?

0 Kudos
HildermesJoséMedeirosFilho
New Contributor III

https://github.com/hildermesmedeiros/run-ipynb

I`ve been using that toolbox, for months I did it while ago, you all feel free to use it. But it would not hurt to be a default toolbox, right?

But to do so, it would need more things, it is working for me, so...

ps: My Github project is now public....I`m sorry for that!

0 Kudos