python script to kill and start itself

亡梦爱人 提交于 2019-12-11 12:30:47

问题


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)

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.

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

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

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.

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...


回答1:


A better way would be to use:

  child = subprocess.Popen(['python','/home/pi/script.py'],shell=True,stdout=subprocess.PIPE)
  output,error = child.communicate()
  print output, error
  sys.exit()



回答2:


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)


来源:https://stackoverflow.com/questions/35831291/python-script-to-kill-and-start-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!