Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...