In my previous article, I introduced the Python subprocess module, explained how to use it, and described when to use it. This article will review two different use cases for the subprocess library: running simple Bash commands and running Bash scripts.
[ Download the Bash cheat sheet to become more efficient at the command line. ]
Use subprocess to run simple Bash commands
You can use the subprocess module to run simple Bash commands such as mkdir
and ls
. This exercise will use Python to parse a text file and create directories based on the folder contents.
First, clone the subprocess_demo repository into your terminal:
git clone https://github.com/nicolenlama/subprocess_demo.git
Then cd
to usecase1
.
There should be two files: directories.txt
and create_directories.py
. The directories.txt
file contains the names of your soon-to-be-generated folders. The create_directories.py
file reads the contents of directories.txt
and creates the directories using the subprocess module and mkdir
. I'll review the contents of the script.
The first line in the script imports the subprocess library:
import subprocess
Next, it loops through the directory names in the directories.txt
file and uses the mkdir
command in the run
method to create the directories.
with open("directories.txt", "r") as directories:
for dirs in directories:
subprocess.run("mkdir ./{0}".format(dirs),shell=True, capture_output=True)
Run the ls
command to verify that the script created all the directories. You should see automation
, backup
, development
, production
, and testing
directories.
subprocess.run("ls",shell=True)
Use subprocess to run a Bash script
You can also run a Bash script with the subprocess library. This example uses the usecase2 folder in the subprocess_demo repo. The Bash script in this example (check_ceph.sh
) accesses the backend Ceph cluster for OpenShift Data Foundation via rsh
and runs several Ceph commands to determine the cluster's health and architecture.
[ Do you know the difference between Red Hat OpenShift and Kubernetes? ]
If you do not have an active OpenShift Data Foundation cluster, you can run the Python script using the -i output.txt
flag in the terminal. The Python script, ceph_status_check.py
, uses subprocess.run("check_ceph.sh")
to obtain the output of the Bash script and then process the text to generate critical insights about the cluster. I highly encourage you to open the script in the subprocess_demo repo to understand how to use Python to parse the output from the subprocess.
To run the Python script, enter the following command:
python3 ceph_status_check.py
Or, if you do not have a running OpenShift Cluster, run:
python3 ceph_status_check.py -i output.txt
If the code ran successfully, four metrics appear in the terminal: ceph status, available capacity, number of osds, and number of pgs.
Give it a try
There you have it! You have just examined two different use cases for the subprocess module.
For a deeper dive, please see the subprocess documentation and Jose Vicente Nunez's How to write an Ansible plugin to create inventory files on Enable Sysadmin.
Sobre el autor
Nicole Lama is a first-generation Panamanian-American with a bachelor's in biomedical sciences and a master's in bioinformatics and computational biology. Since 2015, she has worked with academics, tech startups, and Fortune 500 companies to help meet their data extraction, transformation, mobilization, storage, analytics, and visualization needs. She specializes in Python, machine learning, and open source technology.
Navegar por canal
Automatización
Las últimas novedades en la automatización de la TI para los equipos, la tecnología y los entornos
Inteligencia artificial
Descubra las actualizaciones en las plataformas que permiten a los clientes ejecutar cargas de trabajo de inteligecia artificial en cualquier lugar
Nube híbrida abierta
Vea como construimos un futuro flexible con la nube híbrida
Seguridad
Vea las últimas novedades sobre cómo reducimos los riesgos en entornos y tecnologías
Edge computing
Conozca las actualizaciones en las plataformas que simplifican las operaciones en el edge
Infraestructura
Vea las últimas novedades sobre la plataforma Linux empresarial líder en el mundo
Aplicaciones
Conozca nuestras soluciones para abordar los desafíos más complejos de las aplicaciones
Programas originales
Vea historias divertidas de creadores y líderes en tecnología empresarial
Productos
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Servicios de nube
- Ver todos los productos
Herramientas
- Training y Certificación
- Mi cuenta
- Soporte al cliente
- Recursos para desarrolladores
- Busque un partner
- Red Hat Ecosystem Catalog
- Calculador de valor Red Hat
- Documentación
Realice pruebas, compras y ventas
Comunicarse
- Comuníquese con la oficina de ventas
- Comuníquese con el servicio al cliente
- Comuníquese con Red Hat Training
- Redes sociales
Acerca de Red Hat
Somos el proveedor líder a nivel mundial de soluciones empresariales de código abierto, incluyendo Linux, cloud, contenedores y Kubernetes. Ofrecemos soluciones reforzadas, las cuales permiten que las empresas trabajen en distintas plataformas y entornos con facilidad, desde el centro de datos principal hasta el extremo de la red.
Seleccionar idioma
Red Hat legal and privacy links
- Acerca de Red Hat
- Oportunidades de empleo
- Eventos
- Sedes
- Póngase en contacto con Red Hat
- Blog de Red Hat
- Diversidad, igualdad e inclusión
- Cool Stuff Store
- Red Hat Summit