Python Execute Unix / Linux Command Examples

How do I execute standard Unix or Linux shell commands using Python? Is there a command to invoke Unix commands using Python programs?

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements Python
Time N/A
You can execute the command in a subshell using os.system(). This will call the Standard C function system(). This function will return the exit status of the process or command. This method is considered as old and not recommended, but presented here for historical reasons only. The subprocess module is recommended and it provides more powerful facilities for running command and retrieving their results.

ADVERTISEMENTS

os.system example (deprecated)

The syntax is:

import os
os.system("command")

In this example, execute the date command:

import os
os.system("date")

Sample outputs:

Sat Nov 10 00:49:23 IST 2012
0

In this example, execute the date command using os.popen() and store its output to the variable called now:

import os
f = os.popen('date')
now = f.read()
print "Today is ", now

Sample outputs:

Today is  Sat Nov 10 00:49:23 IST 2012

Say hello to subprocess

The os.system has many problems and subprocess is a much better way to executing unix command. The syntax is:

import subprocess
subprocess.call("command1")
subprocess.call(["command1", "arg1", "arg2"])

In this example, execute the date command:

import subprocess
subprocess.call("date")

Sample outputs:

Sat Nov 10 00:59:42 IST 2012
0

You can pass the argument using the following syntax i.e run ls -l /etc/resolv.conf command:

import subprocess
subprocess.call(["ls", "-l", "/etc/resolv.conf"])

Sample outputs:

<-rw-r--r-- 1 root root 157 Nov  7 15:06 /etc/resolv.conf
0

To store output to the output variable, run:

import subprocess
p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print "Today is", output

Sample outputs:

Today is Sat Nov 10 01:27:52 IST 2012

Another example (passing command line args):

import subprocess
p = subprocess.Popen(["ls", "-l", "/etc/resolv.conf"], stdout=subprocess.PIPE)
output, err = p.communicate()
print "*** Running ls -l command ***\n", output

Sample outputs:

*** Running ls -l command ***
-rw-r--r-- 1 root root 157 Nov  7 15:06 /etc/resolv.conf

In this example, run ping command and display back its output:

import subprocess
p = subprocess.Popen(["ping", "-c", "10", "www.cyberciti.biz"], stdout=subprocess.PIPE)
output, err = p.communicate()
print  output

The only problem with above code is that output, err = p.communicate() will block next statement till ping is completed i.e. you will not get real time output from the ping command. So you can use the following code to get real time output:

import subprocess
cmdping = "ping -c4 www.cyberciti.biz"
p = subprocess.Popen(cmdping, shell=True, stderr=subprocess.PIPE)
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:

PING www.cyberciti.biz (75.126.153.206) 56(84) bytes of data.
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=1 ttl=55 time=307 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=2 ttl=55 time=307 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=3 ttl=55 time=308 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=4 ttl=55 time=307 ms
 
--- www.cyberciti.biz ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 307.280/307.613/308.264/0.783 ms

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

References:
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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

ADVERTISEMENTS
13 comments… add one
  • klo Nov 9, 2012 @ 22:22

    307 ms?
    That’s a long interval ;)

  • Someone Nov 10, 2012 @ 13:00

    Hi, please more small and usefull examples with python like it! more code snippets!

  • meow Dec 6, 2012 @ 12:39

    A very comprehensive explanation, being useful to beginners to python.

  • javith Jan 7, 2013 @ 10:38

    where to find the command of linux

  • Gobinda Singh May 12, 2013 @ 9:22

    these commands are very helpfull us….please give more example like this.

  • Fl'âme Jan 22, 2014 @ 10:06

    Thanks!

  • nanda kishore Mar 25, 2015 @ 6:07

    What exactly does Shell=True does?
    please tell the exact usage of the shell argumet.

  • iSh0w Jul 16, 2015 @ 5:04

    Hi,

    First off, enjoy all your posts and youtube videos. Recently viewed your tutorial on installing freebsd. So thank you for sharing your knowledge.

    I have a query regarding launching an external bash script file (.sh) in freebsd.
    For linux I used:
    os.system(‘sh ‘ + filepath)
    For Mac:
    os.system(‘open ‘ + filepath)
    And for windows:
    os.startfile(filepath)

    I am unable to get any of these to work for freebsd. I know startfile is only for windows, however was wondering if there was an equivalent for freebsd without using subprocess. Or if not possible at all how to use subprocess to call a external script.

    Also, in freebsd, what would be the equivalent of say:
    sudo chown -R user:user file.bundle
    as both sudo and chown are not installed by default.

    Any help would be appreciated.

    Regards,
    iSh0w

  • alimp5 Oct 16, 2015 @ 4:24

    tnxxxxxxx

  • Pete Feb 12, 2016 @ 14:11

    What if I want to create a variable in Python, then pass that variable to a bash command line?
    Something like this:
    ….
    celsius = sensor.read_temperature()
    import subprocess
    subprocess.call([“myscript.sh”, “-v”, “-t $celsius”])

    Is that possible?

    • Lior Redlus Feb 16, 2016 @ 9:53

      Of course you can. In python’s new formatting it would look like this:
      subprocess.call(["myscript.sh", "-v", "-t {}".format(celsius)])

  • Michael Messmer Jul 25, 2016 @ 17:28

    I use split so I dont have to write literal arrays. Works most of the time.

    10849769955f9550e26f417_000011

    • Paul McGuire Aug 24, 2017 @ 12:03

      I also find `”my command goes here”.split()` easier to use and maintain. To get proper handling of quoted args, use shlex.split: `shlex.split(”’this is my “quoted string” argument”’)`, which gives `[‘this’, ‘is’, ‘my’, ‘quoted string’, ‘argument’]`

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.