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

Posted on in Categories , , last updated April 2, 2016

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?

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:

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

The basic syntax is:

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

Run ping command to send ICMP ECHO_REQUEST packets to

import subprocess["ping", "-c 2", ""])

Sample outputs:

PING ( 56 data bytes
64 bytes from icmp_seq=0 ttl=53 time=250.416 ms
64 bytes from icmp_seq=1 ttl=53 time=264.461 ms
--- 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:

## 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:

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 =
    if out == '' and p.poll() != None:
    if out != '':

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    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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

3 comment

  1. 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


Leave a Comment