<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>administration &#8211; VeriTeknik</title>
	<atom:link href="https://www.veriteknik.net.tr/en/tag/administration/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.veriteknik.net.tr/en/</link>
	<description>VeriTeknik Bilişim &#124; VeriTeknik Bilişim</description>
	<lastBuildDate>Tue, 22 May 2012 12:12:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>System Control Over Python With Pipes</title>
		<link>https://www.veriteknik.net.tr/en/system-control-over-python-with-pipes/</link>
					<comments>https://www.veriteknik.net.tr/en/system-control-over-python-with-pipes/#respond</comments>
		
		<dc:creator><![CDATA[Mustafa Emre Aydın]]></dc:creator>
		<pubDate>Tue, 22 May 2012 12:12:21 +0000</pubDate>
				<category><![CDATA[LINUX]]></category>
		<category><![CDATA[Linux Optimization]]></category>
		<category><![CDATA[Programmin]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[pipe]]></category>
		<category><![CDATA[popen]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">http://www.plugged.in/?p=422</guid>

					<description><![CDATA[<p>Even though major system scripting is usually done via bash (or other shell) scripting, it&#8217;s almost as common to see Python as a system administration scripting platform. Today, most major installers, daemons and package management software are written in Python. While using Python as a system scripting tool, it&#8217;s essential to keep using the standard [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.veriteknik.net.tr/en/system-control-over-python-with-pipes/">System Control Over Python With Pipes</a> appeared first on <a rel="nofollow" href="https://www.veriteknik.net.tr/en/">VeriTeknik</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Even though major system scripting is usually done via bash (or other shell) scripting, it&#8217;s almost as common to see Python as a system administration scripting platform. Today, most major installers, daemons and package management software are written in Python.</p>
<p>While using Python as a system scripting tool, it&#8217;s essential to keep using the standard gnu tools if possible, since these tools are reliable, fast and have been tested for years by thousands of other system administrators as well.</p>
<p>The best way to keep using your good old fashioned gnu tools via Python is opening pipes to such tools. Even though it is possible to use the <strong>os</strong> module to do such tasks, the <strong>subprocess</strong> module is more reliable and you have more control over the tasks.</p>
<p>Below you&#8217;ll see two different methods to approach the &#8220;echo&#8221; tool via Python.</p>
<pre class="brush: python; gutter: true; first-line: 1">&gt;&gt;&gt; import os, subprocess
&gt;&gt;&gt; os.system("echo bla")
bla
0
&gt;&gt;&gt; subprocess.Popen(["echo","bla"],stdout=subprocess.PIPE).communicate()[0]
'bla\n'</pre>
<p>As you can see, both give us more or less the same output, but the subprocess module gives us a lot more control. First of all it has pipe support, universal newline support, better handling of exceptions etc.</p>
<p>Below is a method I use to determine the number of processor on the system. Using this, you can find out if you have multiple cores on the system.</p>
<pre class="brush: python; gutter: true; first-line: 1">cpu_num=subprocess.Popen(["grep","-c","processor","/proc/cpuinfo"],stdout=subprocess.PIPE).communicate()[0]</pre>
<p>As you can see, what is does is very very simple, it just counts the number of word &#8220;processor&#8221; in the <strong>/proc/cpuinfo</strong> file using grep.</p>
<p>Don&#8217;t forget that it is very easy to pipe commands through each other with popen, here&#8217;s an example.</p>
<pre class="brush: python; gutter: true; first-line: 1">&gt;&gt;&gt; import subprocess
&gt;&gt;&gt; p1=subprocess.Popen(["cat","/proc/cpuinfo"],stdout=subprocess.PIPE)
&gt;&gt;&gt; p2=subprocess.Popen(["grep","-B","2","-A","2","processor"],stdin=p1.stdout,stdout=subprocess.PIPE)
&gt;&gt;&gt; output = p2.communicate()[0]
&gt;&gt;&gt; print output
processor : 0
vendor_id : GenuineIntel
cpu family : 15</pre>
<p>Using the popen, it is very easy to get information on processes via the pgrep and ps tools. Below is a script when run with the valid parameters, checks if any of it&#8217;s processes uses more (or equal) to the percentage of the CPU specified, if true, kills the process and tries to send a message to the affiliated terminal window.</p>
<p>To accomplish this, we get the pid&#8217;s of the processes via pgrep, send it to ps so to check the cpu percentage and which tty (or pts) it&#8217;s running on. After the check, if necessary, the PID is killed and a message is sent to the terminal it was running on.</p>
<pre class="brush: python; gutter: true; first-line: 1">#!/usr/bin/python
import subprocess, sys, string

if len(sys.argv) != 3 :
    print "Usage : cpukiller.py &lt;process-to-kill&gt; &lt;max-cpu-percentage&gt;"
    raise SystemExit

proc_to_kill = sys.argv[1]
cpu_to_kill = sys.argv[2]

for i in cpu_to_kill :
    if i not in string.digits :
        print "max-cpu-percentage must consist of only digits."
        raise SystemExit

cpu_to_kill = int(cpu_to_kill)

try :
	a=subprocess.Popen(["pgrep",proc_to_kill],stdout=subprocess.PIPE).communicate()[0]
	if a == '' :
		raise SystemExit
	else :
		procc = subprocess.Popen(["ps --no-headers -o pid,pcpu,tname -p $(pgrep %s)"%proc_to_kill],shell=True,stdout=subprocess.PIPE).communicate()[0]
		procc=procc.strip()
except : raise SystemExit 
for lines in procc.split('\n') :
    if lines != '' :
        l=lines.split()
        if int(l[1].split('.')[0]) &gt;= cpu_to_kill :
            if l[2] != '?' :
                try :
                    terminal = open('/dev/'+l[2],'a')
                    subprocess.Popen(["echo","You have exceeded your CPU limit, process with PID %s will be terminated"%l[0]],stdout=terminal).communicate()[0]
                except :
                    print "Couldn't tell the user."
                    pass
            try :
                killer = subprocess.Popen(["kill","-9",l[0]],stdout=subprocess.PIPE).communicate()[0]
            except :
                print "Couldn't kill the process."
                pass
        else : pass</pre>
<p>Try to use it, understand it, and expand it!</p>
<p>The post <a rel="nofollow" href="https://www.veriteknik.net.tr/en/system-control-over-python-with-pipes/">System Control Over Python With Pipes</a> appeared first on <a rel="nofollow" href="https://www.veriteknik.net.tr/en/">VeriTeknik</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.veriteknik.net.tr/en/system-control-over-python-with-pipes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">422</post-id>	</item>
	</channel>
</rss>
