杀死并启动自己的python脚本 [英] python script to kill and start itself

查看:700
本文介绍了杀死并启动自己的python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在小时内达到指定时间后启动我的python脚本的新实例,并杀死当前实例. 使用crontab引导时,python脚本会自动启动.无限while循环读取数据.如果在59秒30分和59秒59分之间有数据输入,文件将关闭,我希望脚本使用新的进程ID启动自身的新实例并杀死旧进程.目前,我正在使用

I want to start a new instance of my python script when it reaches a specified time during the hour and kill the current instance. The python script starts automatically with boot using a crontab. An infinite while loop reads data. If there is data incoming between minute 59 second 30 and minute 59 second 59, the file gets closed and I want the script to start a new instance of itself with a new process ID and kill the old process. At the moment I am doing this by using

  subprocess.call(['python', '/home/pi/script.py'])
  sys.exit(1)

这将启动python脚本的新实例,但是当我使用top检查进程时,旧实例仍然可见并且(似乎)处于活动状态.

That starts a new instance of the python script, but the old one is still visible and (it seems) active when I check the processes with top.

是否有更好的方法让程序启动并在python脚本中杀死自己?

Is there a better way to let the program start and kill itself inside the python script?

另一种方法,使用由python脚本调用的bash脚本:

Another approach using a bash script called by the python script:

#!/bin/sh

PID=`ps -eaf | grep python | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
  echo "killing $PID"
  kill -9 $PID
  sleep 1
  sudo python /home/pi/readserial_V4_RP.py &
  exit 1
fi

但是那是行不通的,因为python脚本在bash脚本可以杀死它之前就结束了.我可以在不杀死python的情况下启动python脚本 进程,但是在启动python脚本的新实例之前,如何确定没有python脚本正在运行.

But that can't work because the python script end, before the bash script can kill it. I could start the python script without killing python processes, but how can I be sure that no python script is running, before I start the new instance of the python script.

因为我感觉到,当我运行多个相同的python脚本时,启动的第一个脚本正在执行所有的工作",而其他的则处于闲置状态...

because I have the feeling, when I have more than one same python scripts running, that the first one that was started is doing all the "work" and the others are just in idle...

推荐答案

拥有运行实际脚本的瘦父脚本,而不是让进程尝试自行重启.父监视器只要退出就重新启动脚本.由于父级会等到孩子完全退出后,您就不会再运行多个版本了.在完整的实现中,父级拥有您需要的任何系统守护程序代码,而子级则专注于任务.

Instead of having a process try to restart itself, have a thin parent script that runs the real script. The parent monitor just restarts the script whenever it exits. Since the parent waits until the child has fully exited, you won't have multiple versions running. In a full implementation, the parent holds any system daemon code you need and the child is focused on the task.

此示例运行一个非常无聊的程序,该程序休眠了命令行中指定的秒数

This example runs a very boring program that sleeps for the number of seconds specified on the command line

import multiprocessing
import time
import sys
import os
import traceback

def the_real_work_done_here(sleep_time):
    try:
        print(os.getpid(), 'start work')
        time.sleep(sleep_time)
        print(os.getpid(), 'end work')
        exit(0)
    except Exception:
        traceback.print_exc()
        exit(2)

def monitor(sleep_time):
    while 1:
        proc = multiprocessing.Process(target=the_real_work_done_here,
            args=(sleep_time,))
        proc.start()
        proc.join()
        if proc.exitcode != 0:
            print('something went wrong... terminating')
            exit(proc.exitcode)


if __name__ == '__main__':
    try:
        sleep_time = int(sys.argv[1])
        monitor(sleep_time)
    except (KeyError, ValueError):
        print('usage: test.py sleeptime')
        exit(1)

这篇关于杀死并启动自己的python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆