0

one script starts automatically when my raspberry is booted up, within this script there is motion sensor, if detected, it starts a subproces camera.py (recording a video, then converts the video and emails)

within the main script that starts u on booting up, there is another if statement, if button pressed then stop the camera.py and everything in it and do something else.

I am unable to kill process by PID because it keeps changing. The only other option is to kill camera.py by its name, but it doesn't work.

main script:

p1 = subprocess.Popen("sudo python /home/pi/camera.py", shell=True)

this is my camera.py script:

import os
os.system("raspivid -n -o /home/pi/viseo.h264 -t 10000")
os.system(.... python script0.py
os.system(.... python script1.py

i can do:

os.system("sudo killall raspivid")

if i try

os.system("sudo killall camera.py")

it gives me a message: No process found

this only stops the recording but i also want to kill every other script within camera.py

Can anyone help please? thanks

3 Answers 3

2

Use pkill:

$ sudo pkill -f camera.py 
Sign up to request clarification or add additional context in comments.

5 Comments

Sweet ! much better than my option
Thanks! But it could be dangerous sometimes because it uses pattern matching.
Thanks!!!!!!! this works, however didnt stop the recording but killed every other script within the camera.py
you should only use this method if absolutely necessary, brute force closing applications/scripts is waiting for problems and/or corruption of data.
@HeadhunterXamd Yes I've already mentioned that it's quite dangerous sometimes.
1

If you make camera.py executable, put it on your $PATH and make line 1 of the script #!/usr/bin/python, then execute camera.py without the python command in front of it, your "sudo killall camera.py" command should work.

Comments

0

instead of using:

import os
os.system("raspivid -n -o /home/pi/viseo.h264 -t 10000")
os.system(.... python script0.py)
os.system(.... python script1.py)

you should use the same Popen structure as how you spawn this process. This gives you access to the Popen object of the calls.

import os 
pvid = subprocess.Popen("raspivid -n -o /home/pi/viseo.h264 -t 10000")
p1 = subprocess.Popen(.... python script0.py)
p2 = subprocess.Popen(.... python script1.py)

Then you can get the pid of all the different scripts and kill them through that.

This should actually be done through an shutdown sequence. You should never Force close applications if you can let it close itself.

1 Comment

From the subprocess docs: "This module intends to replace several older modules and functions: os.system os.spawn* os.popen* popen2.* commands.*"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.