Python execute shell linux

Python Execute Unix / Linux Command Examples

H ow 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 level Easy
Root privileges No
Requirements Python
Est. reading 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.

os.system example (deprecated)

In this example, execute the date command:

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

Say hello to subprocess

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

In this example, execute the date command:

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

Another example (passing command line args):

In this example, run ping command and display back its 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:

A quick video demo of above python code:

References:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

307 ms?
That’s a long interval 😉

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

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

where to find the command of linux

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

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

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.

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?

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

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

Источник

How to Execute Shell Commands with Python

Python is a wonderful language for scripting and automating workflows and it is packed with useful tools out of the box with the Python Standard Library. A common thing to do, especially for a sysadmin, is to execute shell commands. But what usually will end up in a bash or batch file, can be also done in Python. You’ll learn here how to do just that with the os and subprocess modules.

Using the os Module

The first and the most straight forward approach to run a shell command is by using os.system():

If you save this as a script and run it, you will see the output in the command line. The problem with this approach is in its inflexibility since you can’t even get the resulting output as a variable. You can read more about this function in the documentation.

Note, that if you run this function in Jupyter notebook, you won’t have an output inline. Instead you the inline output will be the return code of the executed programm ( 0 for successful and -1 for unsuccessful). You will find the output in the command line where you have started Jupyter notebook.

Next, the os.popen() command opens a pipe from or to the command line. This means that we can access the stream within Python. This is useful since you can now get the output as a variable:

When you use the .read() function, you will get the whole output as one string. You can also use the .readlines() function, which splits each line (including a trailing \n ). Note, that you can run them only once. It is also possible to write to the stream by using the mode=’w’ argument. To delve deeper into this function, have a look at the documentation.

In this example and in the following examples, you will see that you always have trailing line breaks in the output. To remove them (including blank spaces and tabs in the beginning and end) you can use the .strip() function like with output.strip() . To remove those characters only in the beginning use .lstrip() and for the end .rstrip() .

Using the subprocess Module

The final approach is also the most versatile approach and the recommended module to run external commands in Python:

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes. (Source)

The main function you want to keep in mind if you use Python >= 3.5 is subprocess.run(), but before we get there let’s go through the functionality of the subprocess module. The subprocess.Popen() class is responsible for the creation and management of the executed process. In contrast to the previous functions, this class executes only a single command with arguments as a list. This means that you won’t be able to pipe commands:

You’ll notice that we set stdout and stderr to subprocess.PIPE. This is a special value that indicates to subprocess.Popen that a pipe should be opened that you can then read with the .communicate() function. It is also possible to use a file object as with:

Another thing that you’ll notice is that the output is of type bytes. You can solve that by typing stdout.decode(‘utf-8’) or by adding universal_newlines=True when calling subprocess.Popen .

When you run .communicate() , it will wait until the process is complete. However if you have a long program that you want to run and you want to continuously check the status in realtime while doing something else, you can do this like here:

You can use the .poll() function to check the return code of the process. It will return None while the process is still running. To get the output, you can use process.stdout.readline() to read a single line. Conversely, when you use process.stdout.readlines() , it reads all lines and it also waits for the process to finish if it has not finished yet. For more information on the functionionality of subprocess.Popen , have a look at the documentation.

Also note, that you won’t need quotations for arguments with spaces in between like ‘\»More output\»‘ . If you are unsure how to tokenize the arguments from the command, you can use the shlex.split() function:

You have also the subprocess.call() function to your disposal which works like the Popen class, but it waits until the command completes and gives you the return code as in return_code = subprocess.call([‘echo’, ‘Even more output’]) . The recommended way however is to use subprocess.run() which works since Python 3.5. It has been added as a simplification of subprocess.Popen . The function will return a subprocess.CompletedProcess object:

You can now find the resulting output in this variable:

Similar to subprocess.call() and the previous .communicate() function, it will wait untill the process is completed. Finally, here is a more advanced example on how to access a server with ssh and the subprocess module:

Here you can see how to write input to the process. In this case you need to set the bufsize=0 in order to have unbuffered output. After you are finished writing to the stdin , you need to close the connection.

Источник

Executing Shell Commands with Python

Python is an excellent scripting language. More and more sysadmins are using Python scripts to automate their work.

Since the sysadmin tasks involve Linux commands all the time, running Linux commands from the Python script is a great help.

In this tutorial, I’ll show you a couple of ways you can run shell commands and get its output in your Python program.

Execute Shell command in Python with os module

Let me create a simple python program that executes a shell command with the os module.

Now, if I run this program, here’s what I see in the output.

That’s the content of the directory where prog.py is stored.

If you want to use the output of the shell command, you can store it in a file directly from the shell command:

You can also store the output of the shell command in a variable in this way:

If you run the above program, it will print the content of the variable myCmd and it will be the same as the output of the ls command we saw earlier.

Now let’s see another way of running Linux command in Python.

Execute shell command in Python with subprocess module

A slightly better way of running shell commands in Python is using the subprocess module.

If you want to run a shell command without any options and arguments, you can call subprocess like this:

The call method will execute the shell command. You’ll see the content of the current working directory when you run the program:

If you want to provide the options and the arguments along with the shell command, you’ll have to provide them in a list.

When you run the program, you’ll see the content of the current directory in the list format.

Now that you know how to run shell command with subprocess, the question arises about storing the output of the shell command.

For this, you’ll have to use the Popen function. It outputs to the Popen object which has a communicate() method that can be used to get the standard output and error as a tuple. You can learn more about the subprocess module here.

When you run the program, you’ll see the stdout and stderr (which is none in this case).

I hope this quick tip helped you to execute shell command in Python programs. In a related quick tip, you can learn to write list to file in Python.

If you have questions or suggestions, please feel free to drop a comment below.

Источник

Выполнение shell команд с Python

Повторяющиеся задачи созрели для автоматизации. Разработчики и системные администраторы обычно автоматизируют рутинные задачи, такие как проверки работоспособности и резервное копирование файлов, с помощью сценариев оболочки. Однако, поскольку эти задачи становятся более сложными, сценарии оболочки могут усложняться в обслуживании.

К счастью, мы можем использовать Python вместо сценариев оболочки для автоматизации. Python предоставляет методы для запуска команд оболочки, предоставляя нам ту же функциональность, что и сценарии оболочки. Изучение того, как выполнять команды оболочки в Python, открывает нам возможность автоматизировать компьютерные задачи структурированным и масштабируемым образом.

В этой статье мы рассмотрим различные способы выполнения команд оболочки в Python и идеальную ситуацию для использования каждого метода.

Использование os.system для запуска команды

Python позволяет нам немедленно выполнить команду оболочки, которая хранится в строке, используя функцию os.system() .

Давайте начнем с создания нового файла Python с именем echo_adelle.py и введите следующее:

Первое, что мы делаем в нашем Python файле, это импортируем модуль os , который содержит функцию system , которая может выполнять команды оболочки. Следующая строка делает именно это, запускает команду echo в нашей оболочке через Python.

В вашем терминале запустите этот файл с помощью следующей команды, и вы должны увидеть соответствующий вывод:

По мере того, как команды echo выводятся в наш stdout , os.system() также возвращает код завершения команды оболочки. Код 0 означает, что он работает без проблем, а любое другое число означает ошибку.

Давайте создадим новый файл с именем cd_return_codes.py и введите следующее:

В этом сценарии мы создаем две переменные, в которых хранятся результаты выполнения команд, которые изменяют каталог на домашнюю папку и на несуществующую папку. Запустив этот файл, мы увидим:

Первая команда, которая изменяет каталог на домашний каталог, выполняется успешно. Следовательно, os.system() возвращает код ноль, который хранится в home_dir . С другой стороны, unknown_dir сохраняет код завершения неудачной команды bash, чтобы изменить каталог на несуществующую папку.

Функция os.system() выполняет команду, печатает любой вывод команды на консоль и возвращает код завершения команды. Если нам нужно более детальное управление вводом и выводом команды оболочки в Python, мы должны использовать модуль subprocess .

Выполнение команды с подпроцессом

Модуль subprocess — это рекомендуемый Python способ выполнения команд оболочки. Это дает нам гибкость для подавления вывода команд оболочки или цепочки входов и выходов различных команд вместе, в то же время обеспечивая аналогичный опыт os.system() для базовых сценариев использования.

В новом файле с именем list_subprocess.py напишите следующий код:

В первой строке мы импортируем модуль subprocess , который является частью стандартной библиотеки Python. Затем мы используем функцию subprocess.run() для выполнения команды. Также как и команда os.system() , subprocess.run() возвращает код того, что было выполнено.

Обратите внимание, что subprocess.run() принимает список строк в качестве входных данных вместо одной строки. Первым элементом списка является название команды. Остальные пункты списка — это флаги и аргументы команды.

Примечание: Как правило, вам нужно отделить аргументы , основанные на пространстве, например , ls -alh будет [«ls», «-alh»] , а ls -a -l -h , превратится в [«ls», «-a», -«l», «-h»] .

Запустите этот файл, и вывод вашей консоли будет похож на:

Теперь давайте попробуем использовать одну из более продвинутых функций subprocess.run() , а именно игнорирование вывода в stdout . В том же файле list_subprocess.py измените:

Стандартный вывод команды теперь передается на специальное устройство /dev/null , что означает, что вывод не будет отображаться на наших консолях. Запустите файл в вашей оболочке, чтобы увидеть следующий вывод:

Что если мы хотим получить результат команды? subprocess.run() поможет сделать это. Создайте новый файл с именем cat_subprocess.py , набрав следующее:

Мы используем довольно много параметров, давайте рассмотрим их:

  1. stdout=subprocess.PIPE говорит Python перенаправить результат выполнения команды в объект, чтобы позже его можно было прочитать вручную
  2. text=True возвращает stdout и в stderr виде строк. Тип возвращаемого значения по умолчанию — байты.
  3. input=»Hello from the other side» говорит Python добавить строку в качестве ввода в команду cat .

Запуск этого файла приводит к следующему выводу:

Мы также можем бросить Exception без проверки значения возврата. В новом файле false_subprocess.py добавьте код ниже:

В вашем терминале запустите этот файл. Вы увидите следующую ошибку:

Используя check=True , мы сообщаем Python, что нужно вызывать любые исключения, если возникает ошибка. Так как мы столкнулись с ошибкой, оператор print в последней строке не был выполнен.

Функция subprocess.run() дает нам огромную гибкость. Эта функция представляет собой упрощенную абстракцию класса subprocess.Popen , которая предоставляет дополнительные функциональные возможности, которые мы можем исследовать.

Выполнение команды с Popen

Класс subprocess.Popen предоставляет больше возможностей для разработчика при взаимодействии с оболочкой. Тем не менее, мы должны быть более точными в получении результатов и ошибок

По умолчанию subprocess.Popen не останавливает обработку программы Python, если ее команда не завершила выполнение. В новом файле с именем list_popen.py введите следующее:

Этот код эквивалентен list_subprocess.py . Он запускает команду с помощью subprocess.Popen и ожидает ее завершения, прежде чем выполнить оставшуюся часть сценария Python.

Допустим, мы не хотим ждать завершения выполнения команды оболочки, чтобы программа могла работать над другими вещами. Как узнать, когда команда оболочки закончила выполнение?

Метод poll() возвращает код завершения, если команда закончит работу, или None если он все еще выполняется. Например, если бы мы хотели проверить, завершено ли list_dir , а не ждать его, у нас была бы следующая строка кода:

Для управления вводом и выводом subprocess.Popen нам нужно использовать метод communicate() .

В новый файл с именем cat_popen.py добавьте следующий фрагмент кода:

Метод communicate() принимает аргумент input , который используется для передачи входных данных команде оболочки. Метод communicate также возвращает stdout и stderr когда они установлены.

Мы рассмотрели три способа запуска команд оболочки в Python с использованием класса subprocess.Popen . Давайте еще раз рассмотрим их характеристики, чтобы узнать, какой метод лучше всего подходит для требований проекта.

Какой из них я должен использовать?

Если вам нужно выполнить одну или несколько простых команд и вам не помешает, если их вывод поступит в консоль, вы можете использовать команду os.system() . Если вы хотите управлять вводом и выводом команды оболочки, используйте subsystem.run() . Если вы хотите выполнить команду и продолжить выполнять другую работу, пока она выполняется, используйте subprocess.Popen .

Вот таблица с некоторыми различиями в юзабилити, которые вы также можете использовать для обоснования своего решения:

Источник

Читайте также:  Почему ноутбук долго запускается windows
Оцените статью