Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

How to stop an external program started by python on linux

I previously asked how to stop a program and was told/helped, however I neglected to mention the program was external. I wish to press a macro/key and have a external program run, and when pushed again it is stopped/halted all from this code. Eventually I hope to have many macro-keys toggling different programs on/start and off/stop. I have searched and come up with a post from 2016 I think, I have adapted the code slightly, I have it working(starting) an external program but not stopping it. Code is run on Linux and written in python, it follows this, the EXTERNAL program is not killed or stopped.

What bug have I introduced, how to do it.

#!/usr/bin/env python
# Toggler for macro key -|- many keys = one toggler program
import os
import sys
import time
import subprocess


def startp(program):
    return subprocess.Popen(program)


def exitp(p):
    #os.close(program)#requires integer
    p.terminate()


program = startp("firefox")
time.sleep(5)
# program = 'firefox'
exitp(program)

Perhaps someone can help find the bug in my program that I have adapted.

skeptic


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...