Cookbook > Subprocess. Redirection. Interruptions.
Author: Aleksandr Chirkov
Feb. 18, 2014, 1:30 a.m.

Bash is quite useful for work with it from shell because of he has a couple methods which allow you do things easy and fast. Redirection is most useful thing. You use it everytime when work with shell.

Shell allows you redirect output and errors to places which you want, run command with file like stdin  and so on... Let's try a few examples:

 

1. Execute ls command in ordinary mode. Stdout will your shell: 

root@ansible:~# ls -l
total 2036
-rw-r--r--  1 root root    107 Feb  4 16:10 1
drwxrwxrwx  6 dpi  dpi    4096 Nov 24 17:11 ansible
-rw-rw-r--  1 dpi  dpi      25 Jan  9 18:28 bla
drwxrwxr-x  3 dpi  dpi    4096 Feb 14 11:42 bre
-rw-rw-r--  1 dpi  dpi      10 Dec 19 17:02 build.txt
drwxrwxr-x  3 dpi  dpi    4096 Dec 19 17:02 coverage


2. Redirect stdout to foo file: 

root@ansible:~# ls -l > foo


3. Execute wc command with stdin in face foo file

root@ansible:~# wc -l < foo
55


4. Run wc command with foo file like stdin and bla file like stdout 

root@ansible:~# wc -l < foo > bla
root@ansible:~# cat bla
55


5. Use pipe for redirect stdout of ls to stdin of grep: 

root@ansible:~# ls -l | grep pyc
drwxrwxr-x  4 dpi  dpi    4096 Dec 19 17:02 pycharm
drwxrwxr-x 11 dpi  dpi    4096 Jan 16 11:29 pycharmhelper
-rw-rw-r--  1 dpi  dpi  137644 Dec 19 17:02 pyparsing.pyc

 

6. Redirect stdout to null file:

root@ansible:~# ls -l > /dev/null


7. Redirect both stdout and stderr to null file:

root@ansible:~# ls -l non_exist_file > /dev/null 2>&1


8. Redirect stdout of ls to many places:

root@ansible:~# ls -l | tee xxx yyy
total 2036
-rw-r--r--  1 root root    107 Feb  4 16:10 1
drwxrwxrwx  6 dpi  dpi    4096 Nov 24 17:11 ansible
-rw-rw-r--  1 dpi  dpi       3 Feb 14 21:34 bla
drwxrwxr-x  3 dpi  dpi    4096 Feb 14 11:42 bre
-rw-rw-r--  1 dpi  dpi      10 Dec 19 17:02 build.txt
drwxrwxr-x  3 dpi  dpi    4096 Dec 19 17:02 coverage

 

9. Check status code of executed program:

root@ansible:~# echo $?
0

 

And now we'll do it in pythonic way. Let's implement it by Python's standart library subprocess. Step by step we'll cover all above examples:

 

1. Subprocess module has call function wich could just execute command and Popen class wich could more interesting actions:

>>> from  subprocess import Popen, PIPE, call
>>> p = Popen(['ls', 'l'], shell=True)
1                      
ansible                      
bla                      
bre                      
build.txt                  
coverage

>>> call(['ls', '-l'], shell=True)
1                       
ansible                 
bla                      
bre                       
build.txt                   
coverage


>>> p = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE)
>>> output = p.communicate()[0]
>>> output.split('\n')
Out[48]: 
['total 2044',
 '-rw-r--r--  1 root root    107 Feb  4 16:10 1',
 'drwxrwxrwx  6 dpi  dpi    4096 Nov 24 17:11 ansible',
 '-rw-rw-r--  1 dpi  dpi       3 Feb 14 21:34 bla',
 'drwxrwxr-x  3 dpi  dpi    4096 Feb 14 11:42 bre',
 '-rw-rw-r--  1 dpi  dpi      10 Dec 19 17:02 build.txt',
 'drwxrwxr-x  3 dpi  dpi    4096 Dec 19 17:02 coverage' 
.....

 

2. Let's redirect stdout ti file:

>>> f = open('/home/dpi/example', 'w')
>>> p = subprocess.Popen(["ls", "-l"], stdout=f)
>>> f.close()

 

3. Save stdin like file and execute wc command with this stdin: 

>>> f = open('/home/dpi/example', 'r')
>>> p = subprocess.Popen(["wc", "-l"], stdin=f)
55
>>> f.close()

 

4. Redirect stdout and stdin to one same place:

>>> fw = open('/home/dpi/writefile', 'w')
>>> fr = open('/home/dpi/example', 'r')
>>> p = subprocess.Popen(["wc", "-l"], stdin=fr, stdout=fw)
>>> for i in fw, fr:
           i.close()
>>> with open('/home/dpi/writefile', 'r') as f:
           print f.readline()

55

 

5. Pipe stdout of ls to stdin of grep

>>> ls = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE)
>>> grep = subprocess.Popen(["grep", "pyc"], stdin=ls.stdout)
drwxrwxr-x  4 dpi  dpi    4096 Dec 19 17:02 pycharm
drwxrwxr-x 11 dpi  dpi    4096 Jan 16 11:29 pycharmhelper
-rw-rw-r--  1 dpi  dpi  137644 Dec 19 17:02 pyparsing.pyc

 

6. Redirect stdout to null file:

>>> f = open('/dev/null', 'w')
>>> ls = subprocess.Popen(["ls", "-l"], stdout=f)
>>> ls.communicate()
Out[78]: (None, None)
>>> f.close()

 

7. Redirect both stdout and stderr  to null file

>>> f = open('/dev/null', 'w')
>>> ls = subprocess.Popen(["ls", "-l", "/home/dpi/non_exist_file"], stdout=f, stderr=f)
>>> ls.communicate()
Out[85]: (None, None)
>>> f.close()

 

>>> f = open('/dev/null', 'w')
>>> ls = subprocess.Popen(["ls", "-l", "/home/dpi/non_exist_file"], stdout=f, stderr=subprocess.STDOUT)
>>> ls.communicate()
Out[90]: (None, None)
>>> f.close()

 

8. Multiplay redirection:

>>> f1 = open('/home/dpi/ggg', 'w')
>>> f2 = open('/home/dpi/hhh', 'w')
>>> ls = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
>>> output = ls.communicate()[0]
>>> output = output.split('\n')
try:
    for i in output:
        for f in f1,f2:
            f.write(i + '\n')
except:
    pass
finally:           
    for i in f1,f2:
        i.close()

 

9. Check status code:

>>> subprocess.check_output("exit 1", shell=True)
Traceback (most recent call last):
   ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1


>>> subprocess.check_call(["ls", "-l"])
0


>>> ls = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
>>> output = ls.communicate()
>>> ls.returncode
0


>>> find = subprocess.Popen(["find", "/", "-name", "bla"])
find: `/sys/kernel/debug': Permission denied
find: `/root': Permission denied
find: `/proc/tty/driver': Permission denied
.................

>>> find.poll()
Out[5]: 1

 

There are a couple interesing methods in subprocess module. You could check it yourself in official docs. It was first entry to processing in Python.

I'll keep to cover this not simple theme in another topics, we'll try multithreading, multiprocessing, signals and so on.

See you. 

pipe
python
subprocess
signals
redirection

In the third example (Save stdin like file and execute wc command with this stdin) it seems that you forgot to set writefile as stdout for wc -l command. Am I right?

In "8. Multiplay redirection:" you have no exception handler. I think it won't work like that..

As for 3th example, stdout redirect to file is not needed. And about 8th, try construction works without except if there is finally keyword. It is not useful because there may be exception...but anyway  finally block will do closing file.

Priligy Capsulas [url=http://costofcial.com]cialis price[/url] Levitra 20mg Club Is Cephalexin Penicillin Pharmacy That Sell Both Viagra And Dapoxetine On Line Macrobid 100mg cialis Viagra Professional Review Prix Cialis 5 Rxdiler Qu Est Ce Que Le Viagra http://costofcial.com - online pharmacy Acheter Du Cialis Moins Cher
many milligrams cialis should take [url=http://cialisle.com]http://cialisle.com[/url] cialis and water cialis online does cialis slow heart rate
side effects of pfizer viagra [url=http://viagrabs.com/]viagra online[/url] what is the current price for viagra buy viagra online whats better cialis viagra or levitra
loans online payday loans payday loans online no credit check payday loans online no credit check
Buy Pain Meds Online With E Check [url=http://costofcial.com]generic cialis[/url] Doxycycline With Overnight Delivery Cheapeast Zentel Store Free Consultation cialis Cialis Comprare In Farmacia Wheretobuylevitrapills Acheter Priligy Dapoxetine Etats http://costofcial.com - generic cialis Provera Cash Delivery Amoxicillin Reactions Generic Viagra Shipped To P O Box
cialis pill splitting [url=http://cialisle.com]http://cialisle.com[/url] can cialis be taken as needed cialis retail price of 5mg cialis
cialis brand uk cialis canada where to buy daily cialis [url=http://cialisoni.com/]cialis online[/url] long term injury from cialis
Cialis 100mg Pills No Perscription Amoxicillian Spanish Pharmacies Online [url=http://costofcial.com]cialis price[/url] Viagra Pas Cher Payement Scuris Pharmacycanada Cheap Cialis And Viagra online pharmacy Discount Propecia Without A Prescription Tadalis Sx Soft Buy Tadalis Sx Soft Order Name Brand Paxil No Rx Rash Amoxicillin http://costofcial.com - cialis Acquistare Viagra In Europa Tretinoin
phentermine 37.5 without doctor's prescription weight loss drugs prescription phentermine 37.5 without doctor's prescription weight loss drugs prescription weight loss medication
diet supplements that work appetite suppressants diet pills that work slimming tablets 2017 best weight loss pills
loans for bad credit history [url=http://paydaycash1loan.com/]cash advance online[/url] payday loans companies cash advance payday loan
buying cialis in malaysia [url=http://cialisle.com]http://cialisle.com[/url] is 5mg of cialis enough buy cialis online can cialis be purchased over the counter
splitting cialis tablet tadalafil online obat cialis 10mg [url=http://cialisoni.com/]cialis coupon[/url] klonopin and cialis

Leave a comment:

Name
Python
Web development
Review Linux
Administration
Provisioning CVS
Windows AWS
Continuous Integration
NoSql Logging Web Servers FileSystems Scala
accurev ansible apach2 apache api application approaches architecture archivation argparse arguments artifactory automation awk aws backup bash batch-scripting beaver block build built-in caching call captcha cartridge case certificates cgi chain client cloud cloudformation cluster cmd coding collections command-line commands compression conditionals conversion convert cookbook copy counter cron crud css cut cygwin daemon datatype date dd debpackage decorator delattr deploy deque dict distributed-file-systems django DNS domain driver dropbox dump elasticcache elasticsearch encryption exit ext4 extra-tags failover file filename filters for form format freetds functions gerrit getattribute gid git globals glusterfs hardware hook iam indexing inheritance init install job-interview jquery ldap linux list locals logging logs logstash lookup magicmethods mail main-menu metadata metalogger mongodb moosefs mount mssql multiplatform multithreading mysql netcat nginx nosql open-ssh openldap openshift os packaging parse partition path pattern patterns permissions pid pil pip pipe playbook pool post post-commit processes production provisioning proxy putty python python-mysql recursively redirection redis register replication repr restore return review rotation scala script search selenium server setattr settings setup shipper signals singleton slots snapshot socket splunk ssh ssh-key ssl storage str string style subprocess sugar super switch syntactic syntaxhighlighter systeminfo tail tar templatetags time tls tune2fs tuple ubuntu unicode unique unix unixodbc usage usecases uuid uwsgi variable vars version vi virtualenv volume web web-server windows with_items __getattr__