0

I have the following command in linux pkill -f ffmpeg which works on my Raspberry Pi's terminal. However it doesn't work in my Python code.

This is what I've tried:

subprocess.run('pkill -f ffmpeg', shell=True)

and

subprocess.run('pkill -f ffmpeg', shell=True)

and

os.system('pkill -f ffmpeg')

However they don't seem t work.

martineau
  • 112,593
  • 23
  • 157
  • 280
MtDemonics
  • 233
  • 1
  • 3
  • 14

1 Answers1

0

You have different options to run external processes and interact with the operating system.

os.popen() opens a pipe from a command, this allows the command to send its output to another command.

import os

stream = os.popen('pkill -f ffmpeg')
output = stream.readlines()

The susbprocess.Popen method from subprocess module was created with the intention of replacing several methods available in the os module, which were not considered to be very efficient.

import subprocess

subprocess.Popen('pkill -f ffmpeg', shell=True) # or 'pkill -n ffmpeg'
ellhe-blaster
  • 1,071
  • 2
  • 10
  • 20
  • 1
    You probably want to get rid of the superfluous `shell=True` while you are at it. See [Actual meaning of `shell=True` in subprocess](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee Mar 07 '22 at 10:18
  • 1
    It's unclear why you think this will solve the OP's problem. You should generally not use bare `Popen` when a `run` will do so this isn't really an improvement at all. – tripleee Mar 07 '22 at 10:19
  • Okay, at first I just think using `run()` with `'pkill -n ffmpeg'` might solve this problem, but after doing some research on using `subprocess` commands, I thought I'd recommend another instance of interacting with the console that could help, the truth is I don't have much experience using this module so I can conclude that you are right – ellhe-blaster Mar 07 '22 at 11:06