Python Run External Command And Get Output On Screen or In Variable

by on March 30, 2011 · 1 comment· LAST UPDATED December 30, 2013

in , ,

I am a new Python user. I wanted to run a Linux or Unix external program. For example, I would like to call an external program called /bin/date with my python script and get the output on screen or store in a variable. How do I display such output and exit status (return code) of /bin/date program using a Python script?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time2m
You need to use the subprocess Python module which allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Another option is to use operating system interfaces provided by Python such as:
os.system
os.spawn*
os.popen*

Call an external program in python and retrieve the output/return code with subprocess

The basic syntax is:

 
import subprocess
subprocess.call("command-name-here")
subprocess.call(["/path/to/command", "arg1", "-arg2"])
 

Run ping command to send ICMP ECHO_REQUEST packets to www.cyberciti.biz:

 
#!/usr/bin/python
import subprocess
subprocess.call(["ping", "-c 2", "www.cyberciti.biz"])
 

Sample outputs:

PING www.cyberciti.biz (75.126.153.206): 56 data bytes
64 bytes from 75.126.153.206: icmp_seq=0 ttl=53 time=250.416 ms
64 bytes from 75.126.153.206: icmp_seq=1 ttl=53 time=264.461 ms
 
--- www.cyberciti.biz ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 250.416/257.438/264.461/7.023 ms

Example: Run external date command and display output on screen

In this example, run a standard Unix date command and display back the output on screen:

 
#!/usr/bin/python
## get subprocess module 
import subprocess
 
## call date command ##
p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
 
## Talk with date command i.e. read data from stdout and stderr. Store this info in tuple ##
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.
(output, err) = p.communicate()
 
## Wait for date to terminate. Get return returncode ##
p_status = p.wait()
print "Command output : ", output
print "Command exit status/return code : ", p_status
 

Sample outputs:

Command output :  Mon Dec 30 23:50:43 IST 2013
Command exit status/return code :  0

How do I get real time output from my commands on screen?

The p.communicate() has a few problems:

  1. The data read is buffered in memory, so do not use this method if the data size is large or unlimited.
  2. It will block next statement till external command is completed i.e. you will not get real time output from the command.

The following program will run netstat unix command and start display output immediately on screen:

 
#!/usr/bin/python
import subprocess
## command to run - tcp only ##
cmd = "/usr/sbin/netstat -p tcp -f inet"
 
## run it ##
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
 
## But do not wait till netstat finish, start displaying output immediately ##
while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()
 

Sample outputs:

Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)
tcp4       0      0  desktop01.52712        proxy1.nixcraftu.http  ESTABLISHED
tcp4       0      0  desktop01.52710        proxy1.nixcraftu.http  ESTABLISHED
tcp4       0      0  desktop01.52701        proxy1.nixcraftu.http  ESTABLISHED
tcp4       0      0  desktop01.52277        proxy1.nixcraftu.http  ESTABLISHED
tcp4       0      0  desktop01.52021        maa03s17-in-f21..https ESTABLISHED
tcp4     146      0  desktop01.49548        maa03s04-in-f0.1.https CLOSE_WAIT
tcp4       0      0  desktop01.49292        ni-in-f125.1e100.jabbe ESTABLISHED
tcp4       0      0  desktop01.49154        10.172.232.154.5223    ESTABLISHED

Related media

A quick video demo of above python code:



HTML 5 Video 01: Python Run External Command And Get Output On Screen or In Variable

See also
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 1 comment… read it below or add one }

1 Vineeth March 29, 2014 at 4:25 am

Hi
I use python subprocess module on windows 7 & 8 machines
Found return codes are different when compared

Windows 7: command executed gives the output & return code is 0
Windows 8: command executed gives the output & return code is 255
Both Windows 7 and 8 gives me same expected output.

I know if return code = 0, command executed successfully
What if return code is +ve and what if return code is -ve.

Appreciate your efforts

Thanks

Reply

Leave a Comment

Tagged as: , , ,

Previous Faq:

Next Faq: