简体   繁体   English

Python检查其他脚本是否正在运行并停止它

[英]Python check if another script is running and stop it

I have several python scripts that turn my TV on and off. 我有几个可以打开和关闭电视的python脚本。 Sometimes the TV does not respond the first time so I use a while loop to continue sending the command until the "success" response is sent. 有时电视第一次没有响应,因此我使用while循环继续发送命令,直到发送“成功”响应。 Up to 10 times. 最多10次。

I need to check if one of these programs are running when any of them are started and kill the first process. 我需要检查其中任何一个启动时是否正在运行这些程序之一,并杀死第一个进程。

This answer uses domain locks and I think this could work but I dont really understand whats happening there: https://stackoverflow.com/a/7758075/2005444 这个答案使用域锁,我认为这可以工作,但是我真的不明白那里发生了什么: https : //stackoverflow.com/a/7758075/2005444

What I dont know is what the process_name would be. 我不知道什么是process_name。 The scripts are titles tvon.py, tvoff.py, and tvtoggle.py. 脚本的标题为tvon.py,tvoff.py和tvtoggle.py。 Is it just the title? 只是标题吗? Would it include the extension? 它会包括扩展名吗? How do I get the pid so I can kill the process? 如何获取pid,以便终止进程?

This is running on Ubuntu 14.04.1 它在Ubuntu 14.04.1上运行

EDIT: all I really need is to search for any of these running scripts first. 编辑:我真正需要的是首先搜索所有这些正在运行的脚本。 Also, instead of killing the process maybe I could just wait for it to finish. 另外,我可以等待它完成,而不是终止该过程。 I could just do a loop and break it if none of those processes are running. 如果这些进程都没有运行,我可以做一个循环并中断它。

The reason I need to do this is if the tv is off and the off script is run it will never succeed. 我需要这样做的原因是,如果电视关闭并且运行了关闭脚本,它将永远不会成功。 The TV wont respond if it is already off. 如果电视机已经关闭,它将无法响应。 Which is why I built in the limit of 10 commands. 这就是为什么我限制10个命令的原因。 It never really takes over 4 so 10 is overkill. 它永远不会真正占据超过4的位置,因此10是过大的。 The problem is if the off command is trying to run and I turn the TV on using the tvon script the TV will turn on and back off. 问题是,如果要运行off命令,而我使用tvon脚本打开电视,则电视将打开然后关闭。 Although the TV limits how often commands can be accepted, which reduces the chance of this happening I still want the to be as cleanly working as possible. 尽管电视限制了接受命令的频率,但是这减少了发生这种情况的机会,我仍然希望电视能够尽可能干净地工作。

EDIT: I found that I can not kill the process because it can lock the tty port up which requires a manual restart. 编辑:我发现我无法终止该进程,因为它可以锁定需要手动重启的tty端口。 So I think the smarter way is to have the second process wait until the first is done. 因此,我认为更明智的方法是让第二个过程等到第一个过程完成。 Or find a way to tell the first process to stop at a specific point in the loop so I know its not transmitting. 或者找到一种方法来告诉第一个进程停止在循环中的特定点,这样我就知道它没有传输。

If you have a socket, use it. 如果您有插座,请使用它。 Sockets provide full-blown bidirectional communication. 套接字提供全面的双向通信。 Just write your script to kill itself if it receives anything on the socket. 只要编写脚本以杀死自己,如果脚本在套接字上收到任何内容。 This can be most easily done by creating a separate thread which tries to do a socket.recv() (for SOCK_DGRAM ) or socket.accept() (for SOCK_STREAM / SOCK_SEQPACKET ), and then calls sys.exit() once that succeeds. 这可以通过创建一个单独的线程来尝试完成,该线程尝试执行socket.recv() (对于SOCK_DGRAM )或socket.accept() (对于SOCK_STREAM / SOCK_SEQPACKET ),然后在成功后调用sys.exit()

You can then use socket.send() ( SOCK_DGRAM ) or socket.connect() ( SOCK_STREAM / SOCK_SEQPACKET ) from the second script instance to ask the first instance to exit. 然后,您可以使用第二个脚本实例中的socket.send()SOCK_DGRAM )或socket.connect()SOCK_STREAM / SOCK_SEQPACKET )请求第一个实例退出。

This function can kill a python script by name on *nix systems. 该函数可以在* nix系统上按名称杀死python脚本。 It looks through a list of running processes, finds the PID of the one associated with your script, and issues a kill command. 它遍历正在运行的进程列表,查找与您的脚本关联的进程的PID,并发出kill命令。

import subprocess
def killScript(scriptName):
        # get running processes with the ps aux command
        res = subprocess.check_output(["ps","aux"], stderr=subprocess.STDOUT)

        for line in res.split("\n"):
                # if one of the lines lists our process
                if line.find(scriptName) != -1:
                        info = []

                        # split the information into info[]
                        for part in line.split(" "):
                                if part.strip() != "":
                                        info.append(part)

                        # the PID is in the second slot
                        PID = info[1]

                        #kill the PID
                        subprocess.check_output(["kill",PID], stderr=subprocess.STDOUT)

At the beginning of your tv script you could run something like: 在电视脚本的开头,您可以运行以下命令:

killList = ["tvon.py", "tvoff.py", "tvtoggle.py"]
for script in killList:
        killScript(script)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM