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

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

See also

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.