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
Difficulty level Easy
Root privileges No
Requirements Python 2.x/3.x
Est. reading time 3 minutes
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
subprocess.call(["/path/to/command", "arg1", "-arg2"])

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

import subprocess
subprocess.call(["ping", "-c 2", "www.cyberciti.biz"])

Sample outputs:

PING www.cyberciti.biz ( 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
--- 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

For Python 3 compatibility change print to print(). For example:
print("Foo bar", var1)

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

Here is what I see:

Command output :  Mon Dec 30 23:50:43 IST 2013

Command exit status/return code :  0

For Python version 3.x, use:

print("Command output : ",output.decode())
print("Command exit status/return code : ", p_status)

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 = p.stderr.read(1)
    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:

Sorry, your browser doesn’t support HTML5 video.
HTML 5 Video 01: Python Run External Command And Get Output On Screen or In Variable

Summing up

You learned how to all an external program/command using a python script and retrieve the program output and return exit status. See:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Vineeth Mar 29, 2014 @ 4:25

    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


  • Donal Morrissey May 14, 2016 @ 15:35

    Thank you very much, this is very helpful!

  • Lakshminarayana Feb 2, 2017 @ 14:27

    Nice demo with video.
    Thanks, Vivek :)

  • Xavi May 25, 2021 @ 8:28

    It worked for me, thanks so much!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum