<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: set timeout for a shell command in python</title>
	<atom:link href="http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/</link>
	<description>Random code-snippets and tips</description>
	<lastBuildDate>Tue, 17 Aug 2010 19:16:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: philipp.keller</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/comment-page-1/#comment-134286</link>
		<dc:creator>philipp.keller</dc:creator>
		<pubDate>Tue, 17 Aug 2010 19:16:22 +0000</pubDate>
		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comment-134286</guid>
		<description>Hm, I&#039;ve just tested in Python 2.6.1 and here it works. Let me know when you find the reason behind this (IMO a good place to post python questions is &lt;a href=&quot;http://stackoverflow.com&quot; rel=&quot;nofollow&quot;&gt;stackoverflow.com&lt;/a&gt;)</description>
		<content:encoded><![CDATA[<p>Hm, I&#8217;ve just tested in Python 2.6.1 and here it works. Let me know when you find the reason behind this (IMO a good place to post python questions is <a href="http://stackoverflow.com" rel="nofollow">stackoverflow.com</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhishek Kona</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/comment-page-1/#comment-134283</link>
		<dc:creator>Abhishek Kona</dc:creator>
		<pubDate>Tue, 17 Aug 2010 15:51:51 +0000</pubDate>
		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comment-134283</guid>
		<description>Does not seem to work in Python 2.6.5

 process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File &quot;/usr/lib/python2.6/subprocess.py&quot;, line 633, in __init__
    errread, errwrite)
  File &quot;/usr/lib/python2.6/subprocess.py&quot;, line 1139, in _execute_child
    raise child_exception</description>
		<content:encoded><![CDATA[<p>Does not seem to work in Python 2.6.5</p>
<p> process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)<br />
  File &#8220;/usr/lib/python2.6/subprocess.py&#8221;, line 633, in __init__<br />
    errread, errwrite)<br />
  File &#8220;/usr/lib/python2.6/subprocess.py&#8221;, line 1139, in _execute_child<br />
    raise child_exception</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wrybread</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/comment-page-1/#comment-24231</link>
		<dc:creator>wrybread</dc:creator>
		<pubDate>Tue, 04 Mar 2008 23:51:55 +0000</pubDate>
		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comment-24231</guid>
		<description>Also note that the process module has timeout built in. No need to poll, no need to terminate the spawned process:

http://trentm.com/projects/process/

import process
try:
    p = process.ProcessOpen(&quot;notepad.exe&quot;)
    p.wait(timeout=5)
except process.ProcessError:
    print &quot;timeout!&quot;</description>
		<content:encoded><![CDATA[<p>Also note that the process module has timeout built in. No need to poll, no need to terminate the spawned process:</p>
<p><a href="http://trentm.com/projects/process/" rel="nofollow">http://trentm.com/projects/process/</a></p>
<p>import process<br />
try:<br />
    p = process.ProcessOpen(&#8220;notepad.exe&#8221;)<br />
    p.wait(timeout=5)<br />
except process.ProcessError:<br />
    print &#8220;timeout!&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wrybread</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/comment-page-1/#comment-24144</link>
		<dc:creator>wrybread</dc:creator>
		<pubDate>Mon, 03 Mar 2008 08:41:46 +0000</pubDate>
		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comment-24144</guid>
		<description>Small typo in the above Windows code. Should read:

import ctypes
TerminateProcess = ctypes.windll.kernel32.TerminateProcess
TerminateProcess(int(popen._handle), -1)

(Note the underscore added before &quot;handle&quot;).</description>
		<content:encoded><![CDATA[<p>Small typo in the above Windows code. Should read:</p>
<p>import ctypes<br />
TerminateProcess = ctypes.windll.kernel32.TerminateProcess<br />
TerminateProcess(int(popen._handle), -1)</p>
<p>(Note the underscore added before &#8220;handle&#8221;).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Giovanni Bajo</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/comment-page-1/#comment-23576</link>
		<dc:creator>Giovanni Bajo</dc:creator>
		<pubDate>Fri, 22 Feb 2008 15:26:09 +0000</pubDate>
		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comment-23576</guid>
		<description>For Windows, you can use:

import ctypes
TerminateProcess = ctypes.windll.kernel32.TerminateProcess
TerminateProcess(int(process.handle), -1)</description>
		<content:encoded><![CDATA[<p>For Windows, you can use:</p>
<p>import ctypes<br />
TerminateProcess = ctypes.windll.kernel32.TerminateProcess<br />
TerminateProcess(int(process.handle), -1)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lee Steensland</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/comment-page-1/#comment-16866</link>
		<dc:creator>Lee Steensland</dc:creator>
		<pubDate>Tue, 02 Oct 2007 16:40:58 +0000</pubDate>
		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comment-16866</guid>
		<description>With a one more line of code, and a change to another, your function will act exactly like popen: Note, this requires python 2.4+ Enjoy!

[python num=h5,15]
def TIMEOUT_COMMAND(command, timeout):
    &quot;&quot;&quot;call shell-command and either return its output or kill it
    if it doesn&#039;t normally exit within timeout seconds and return None&quot;&quot;&quot;
    import subprocess, datetime, os, time, signal
    cmd = command.split(&quot; &quot;)
    start = datetime.datetime.now()
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    while process.poll() is None:
        time.sleep(0.2)
        now = datetime.datetime.now()
        if (now - start).seconds &gt; timeout:
            os.kill(process.pid, signal.SIGKILL)
            os.waitpid(-1, os.WNOHANG)
            return None
    return process.stdout.readlines()
[/python]</description>
		<content:encoded><![CDATA[<p>With a one more line of code, and a change to another, your function will act exactly like popen: Note, this requires python 2.4+ Enjoy!</p>
<div class="igBar"><span id="lpython-1"><a href="#" onclick="javascript:showPlainTxt('python-1'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-1">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">def</span> TIMEOUT_COMMAND<span style="color: black;">&#40;</span>command, timeout<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #483d8b;">""</span><span style="color: #483d8b;">"call shell-command and either return its output or kill it</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">&nbsp; &nbsp; if it doesn't normally exit within timeout seconds and return None"</span><span style="color: #483d8b;">""</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>, <span style="color: #dc143c;">datetime</span>, <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">time</span>, <span style="color: #dc143c;">signal</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #dc143c;">cmd</span> = command.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">" "</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; start = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; process = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span>, stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span>, stderr=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">while</span> process.<span style="color: black;">poll</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;color:#800000;">0</span>.<span style="color: #ff4500;color:#800000;">2</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; now = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>now - start<span style="color: black;">&#41;</span>.<span style="color: black;">seconds</span>&gt; timeout:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">kill</span><span style="color: black;">&#40;</span>process.<span style="color: black;">pid</span>, <span style="color: #dc143c;">signal</span>.<span style="color: black;">SIGKILL</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">waitpid</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;color:#800000;">1</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">WNOHANG</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> process.<span style="color: black;">stdout</span>.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Rantala</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/comment-page-1/#comment-5307</link>
		<dc:creator>Joe Rantala</dc:creator>
		<pubDate>Fri, 18 May 2007 15:33:44 +0000</pubDate>
		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comment-5307</guid>
		<description>Note that os.kil is not available on windows.  Too bad :-(</description>
		<content:encoded><![CDATA[<p>Note that os.kil is not available on windows.  Too bad <img src='http://code.pui.ch/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: philipp.keller</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/comment-page-1/#comment-1756</link>
		<dc:creator>philipp.keller</dc:creator>
		<pubDate>Fri, 30 Mar 2007 07:35:31 +0000</pubDate>
		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comment-1756</guid>
		<description>Thanks Juan, I fixed the function and the example so the errors won&#039;t occur anymore. Sorry for the inconvenience.</description>
		<content:encoded><![CDATA[<p>Thanks Juan, I fixed the function and the example so the errors won't occur anymore. Sorry for the inconvenience.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Juan Manuel Caicedo</title>
		<link>http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/comment-page-1/#comment-1749</link>
		<dc:creator>Juan Manuel Caicedo</dc:creator>
		<pubDate>Fri, 30 Mar 2007 03:03:58 +0000</pubDate>
		<guid isPermaLink="false">http://code.pui.ch/2007/02/19/set-timeout-for-a-shell-command-in-python/#comment-1749</guid>
		<description>I tried to use the function but I found two problems.

First, the command argument is passed as a string, not a list. Using a string I was having the following error:

OSError: [Errno 2] No such file or directory

Then, a &#039;No such process&#039; error appeared when the process has run for too long. That happend when the os.kill function was called for the second time. I added a &#039;return None&#039; to fix this problem.</description>
		<content:encoded><![CDATA[<p>I tried to use the function but I found two problems.</p>
<p>First, the command argument is passed as a string, not a list. Using a string I was having the following error:</p>
<p>OSError: [Errno 2] No such file or directory</p>
<p>Then, a 'No such process' error appeared when the process has run for too long. That happend when the os.kill function was called for the second time. I added a 'return None' to fix this problem.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
