How do I terminate my subprocesses in Python [duplicate] - python

This question already has answers here:
How to terminate a python subprocess launched with shell=True
(11 answers)
Closed 7 years ago.
I have two subprocesses as I showed below
cmd1='arecord -d 0 -f cd -t wav test.wav'
cmd2='raspivid -o video.h264 -fps 25 -t 0'
pro1 = subprocess.Popen(cmd1, shell=True)
pro2 = subprocess.Popen(cmd2, shell=True)
I want to record audio and video at the same time with raspberry pi. From now on I can start both programs but I cannot stop these programs. Can anybody help me?
Or any idea for doing by simple way this process?

From the docs:
Popen.terminate() Stop the child. On Posix OSs the method sends
SIGTERM to the child. On Windows the Win32 API function
TerminateProcess() is called to stop the child.
New in version 2.6.
Popen.kill() Kills the child. On Posix OSs the function sends SIGKILL
to the child. On Windows kill() is an alias for terminate().

The documentation for Python's subprocess module includes a description of Popen.terminate() and Popen.kill().

Related

Running a python script with inputs from another python script [duplicate]

This question already has answers here:
How do I write to a Python subprocess' stdin?
(5 answers)
Closed 2 years ago.
I have lots of python scripts to be run and I want to automate them. All of them accept inputs at certain times (3 in this case). I have tried something like this but since echo does not have an EOF it did not work:
os.system("echo 4 | echo 5 | echo 6 | python script.py")
I cannot change the content of script.py and it does not accept arguments to it.
How can I automatically input using a line(s) of python code? Thanks.
Check out Pexpect:
Pexpect is a pure Python module for spawning child applications;
controlling them; and responding to expected patterns in their output.
Pexpect works like Don Libes’ Expect. Pexpect allows your script to
spawn a child application and control it as if a human were typing
commands.

How to do multi-process with shell script? [duplicate]

This question already has answers here:
Waiting for background processes to finish before exiting script
(5 answers)
Closed 3 years ago.
I want to write a shell script which can start two python scripts parallelly. Other commands in shell script will run after the two parallel python processes are done.
In conclusion, two key-points:
run the two python processes parallelly;
run other commands after the python processes are done.
How should I do that?
declare -a pids
launch_first_program &
pids+=($!)
launch_second_program &
pids+=($!)
wait "${pids[#]}"
continue_with_our_day
After a process is launched in background with &, the variable $! will contain the child's PID. We can collect it into an array variable, then use wait to block till all of the listed processes are done.
You can use wait in bash to wait for background processes to finish
python script1.py &
python script2.py &
wait
Other commands

How to kill a running python process? [duplicate]

This question already has answers here:
Find and kill a process in one line using bash and regex
(30 answers)
Closed 9 years ago.
How to kill a running python in shell script when we know the python file name xxx.py?
(it is executed by cmd python xxx.py)
I can use ps aux | grep python to get the pid of it. and then
kill pid to terminate it.
but every time, I have to execute two cmd. Is there a good way to do it?
The pkill utility can look at command lines when sending signals:
pkill -f xxx.py

passing commands to application CLI from python script [duplicate]

This question already has answers here:
How to give input streams in python?
(2 answers)
Closed 9 years ago.
To run shell command from python script, I generally use subprocess or os.system module.
Using that I am running some shell command from python script which is initiating another application and that application also has command line interface.
How would I pass commands to that application CLI from my python script?
How can I capture the output of application CLI from my python script?
It is highly appreciated if someone can suggest material or example code.
The application you're initiating might behave differently when running through a subprocess. Specifically, when connected to a process pipe, some applications buffer their output by default instead of flushing line by line. If the application you're running flushes its output, you can get it realtime, otherwise, you'll only get output when the buffer is full.
That said, here's an example to run some application:
p = subprocess.Popen(['someapp', 'param1', 'param2'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,)
# sends the command "some_command" to the app:
p.stdin.write('some_command\n')
# waits for a single line from the output
result = p.stdout.readline()
If it hangs on p.stdout.readline() that means the output is being buffered.

Command Line Execution in Python with subprocess? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Python subprocess
child subprocess kill in python daemon
How can I run commands in the windows command line through python?
I'm trying to have it so that it starts a subprocess, I don't care about the output, but I want to be able to kill it at any time.
subprocess.Popen: http://docs.python.org/library/subprocess.html

Categories

Resources