JasonBourne1
Posts: 6
Joined: Wed Jan 14, 2015 11:46 pm

Python Subprocess issue

Wed Feb 04, 2015 5:56 am

Code: Select all

import os
import signal
import subprocess
import sys
import time

proc = subprocess.Popen(['fbi', '-a', '-T', '2', 'images/ready.png'])
print proc.pid
time.sleep(10)
proc.kill()
When I remove the time.sleep() then the process is killed properly.

When the time.sleep is present then the pid after the program finishes is 1 higher than the pid printed from this script and it is not killed properly.

I want to be able to kill the process after some other code has run.

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Python Subprocess issue

Wed Feb 04, 2015 6:46 am

I use

To start

Import subprocess
Import os

p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid) where rpistr is your command eg "raspistill -t 0 -tl 0 etc.

To stop it use. os.killpg(p.pid, signal.SIGTERM)

JasonBourne1
Posts: 6
Joined: Wed Jan 14, 2015 11:46 pm

Re: Python Subprocess issue

Wed Feb 04, 2015 7:02 am

That doesnt work for me. The process is still alive after exiting the python script and weirdly its process id is one more than printed during the program.

I think its because the fbi process never returns.

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python Subprocess issue

Wed Feb 04, 2015 8:13 am

My guess is that fbi spawns it's own subprocess. I would suggest you google 'killing fbi', but that might elicit a knock on your door in the early hours. Instead, you might look at this: http://stackoverflow.com/questions/2638 ... rom-python

Code: Select all

p=subprocess.Popen(your_command, preexec_fn=os.setsid)
...
os.killpg(os.getpgid(p.pid), 15)

JasonBourne1
Posts: 6
Joined: Wed Jan 14, 2015 11:46 pm

Re: Python Subprocess issue

Wed Feb 04, 2015 8:44 am

I've tried that to no avail.

I've read every thread that appears on the first 5 pages of google.

Hopefully someone has done this before.

Code: Select all

import os
import signal
import subprocess
import sys
import time

cmd = ['fbi', '-a', '-vt', '1', 'images/ready.png']
p = subprocess.Popen(cmd, preexec_fn=os.setsid)
print p.pid
time.sleep(2)
os.killpg(os.getpgid(p.pid), 15)
print "process killed"
produces the following output:
root@testpi1:/home/pi# python tes.py
2613
using "DejaVu Sans Mono-16", pixelsize=16.67 file=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf
process killed
root@testpi1:/home/pi# ps auxf | grep fbi
root 2617 0.0 0.1 3552 808 pts/0 S+ 19:57 0:00 \_ grep fbi
root 2614 0.8 0.4 11832 2196 ? Ss 19:57 0:00 fbi -a -vt 1

The python script reports pid as 2613 but after the script it is 2614

Return to “Troubleshooting”