<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Windows Task Hangs Due to sys.exit Call in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/windows-task-hangs-due-to-sys-exit-call/m-p/1035633#M60340</link>
    <description>&lt;P&gt;I'm diligently troubleshooting now.&amp;nbsp; The main block of code in the script is a try/except/finally block.&amp;nbsp; If I replace the try with pass, the script exits successfully as a task.&amp;nbsp; So, it seems like something from inside the try block is keeping it from exiting.&amp;nbsp; What's interesting is that the task is set to stop the task if it runs over two hours and kill it if it won't stop on its own.&amp;nbsp; That is not working at all.&amp;nbsp; So, I am going through the try block a little bit at a time and running the script.&amp;nbsp; I will let you know if I solve it.&lt;/P&gt;</description>
    <pubDate>Thu, 11 Mar 2021 19:31:04 GMT</pubDate>
    <dc:creator>NathanHeickLACSD</dc:creator>
    <dc:date>2021-03-11T19:31:04Z</dc:date>
    <item>
      <title>Windows Task Hangs Due to sys.exit Call</title>
      <link>https://community.esri.com/t5/python-questions/windows-task-hangs-due-to-sys-exit-call/m-p/1035610#M60339</link>
      <description>&lt;P&gt;We just migrated Python scripts running on an old Windows 7 VM to a Windows 10 VM.&amp;nbsp; Most of them worked just fine, but one script that ends with the following statement hangs when run from Task Scheduler.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sys.exit(0 if successful else 1)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I run it from the command line or IDLE, it completes just fine.&amp;nbsp; It only hangs as a scheduled task.&amp;nbsp; I added print statements on every line at the end and the only one that doesn't execute is the one after sys.exit.&amp;nbsp; So either it is hanging on sys.exit in Python or Task Scheduler is having an issue after the script finishes.&amp;nbsp; This only happens in the script where I have a sys.exit call.&amp;nbsp; I added it to let me know if the script had any errors in it.&amp;nbsp; I catch any exceptions in the script and add them to an email notification, so the script normally ends with return code 0 if I don't do this.&amp;nbsp; I can imagine some workarounds, but I wanted to see why it no longer works anymore.&amp;nbsp; Any one else have experience with this?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Mar 2021 19:27:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/windows-task-hangs-due-to-sys-exit-call/m-p/1035610#M60339</guid>
      <dc:creator>NathanHeickLACSD</dc:creator>
      <dc:date>2021-03-11T19:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: Windows Task Hangs Due to sys.exit Call</title>
      <link>https://community.esri.com/t5/python-questions/windows-task-hangs-due-to-sys-exit-call/m-p/1035633#M60340</link>
      <description>&lt;P&gt;I'm diligently troubleshooting now.&amp;nbsp; The main block of code in the script is a try/except/finally block.&amp;nbsp; If I replace the try with pass, the script exits successfully as a task.&amp;nbsp; So, it seems like something from inside the try block is keeping it from exiting.&amp;nbsp; What's interesting is that the task is set to stop the task if it runs over two hours and kill it if it won't stop on its own.&amp;nbsp; That is not working at all.&amp;nbsp; So, I am going through the try block a little bit at a time and running the script.&amp;nbsp; I will let you know if I solve it.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Mar 2021 19:31:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/windows-task-hangs-due-to-sys-exit-call/m-p/1035633#M60340</guid>
      <dc:creator>NathanHeickLACSD</dc:creator>
      <dc:date>2021-03-11T19:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: Windows Task Hangs Due to sys.exit Call</title>
      <link>https://community.esri.com/t5/python-questions/windows-task-hangs-due-to-sys-exit-call/m-p/1040699#M60555</link>
      <description>&lt;P&gt;After a lot of troubleshooting, I was able to determine that if I comment out a function call to arcpy.management.SelectLayerByLocation, the issue went away.&amp;nbsp; So, I made a simple test script where I just created a feature layer and performed a selection by location and replicated it very clearly.&amp;nbsp; It turns out that the problem did not occur on any other machine.&amp;nbsp; So, we recreated the VM and the problem went away.&amp;nbsp; In the end, this had nothing to do with sys.exit.&amp;nbsp; It was just the last line in the script.&amp;nbsp; The issue was that this one function call would cause the script not to exit only on this machine and only from the command prompt or as a scheduled task.&amp;nbsp; I hope this helps someone else.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 17:43:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/windows-task-hangs-due-to-sys-exit-call/m-p/1040699#M60555</guid>
      <dc:creator>NathanHeickLACSD</dc:creator>
      <dc:date>2021-03-25T17:43:42Z</dc:date>
    </item>
  </channel>
</rss>

