Python3从另一个脚本重启脚本

时间:2017-05-09 13:22:55

标签: python subprocess call

情况就是这样:

我在Linux中运行2个不同shell的python中有2个脚本: 1° - python3 server.py 2° - python3 roomcontrol.py

我需要用户可以从server.py重新启动roomcontrol.py。 我试过子进程:

from subprocess import call

dir = os.path.dirname(os.path.realpath(__file__)) + "/roomcontrol.py"
call(["python3",dir])

这些说明只是在“server.py”的shell中启动了一个新的“roomcontrol.py”,我需要在他的shell中重启roomcontrol.py。或者关闭他的壳并打开一个新的。

编辑:

我也尝试过:

import subprocess

dir = os.path.dirname(os.path.realpath(__file__)) + "/roomcontrol.py"
subprocess.Popen([dir], stdout=subprocess.PIPE, shell=True)

它不起作用。 它在server.py的同一个shell中编写了很多东西,我的光标变成了一个十字架,如果我点击某个地方就像以前一样。它写的一个小例子:

import: unable to grab mouse `': Resource temporarily unavailable @ error/xwindow.c/XSelectWindow/9199.
import: unable to grab mouse `': Resource temporarily unavailable @ error/xwindow.c/XSelectWindow/9199.
.
.
.
from: can't read /var/mail/xml.dom
/home/stark/Desktop/TrackingOk/Release/roomcontrol.py: 9: /home/stark/Desktop/Tr: not foundlease/roomcontrol.py: 
/home/stark/Desktop/TrackingOk/Release/roomcontrol.py: 10: /home/stark/Desktop/T: not foundelease/roomcontrol.py: try:

1 个答案:

答案 0 :(得分:0)

创建一个新的.sh文件(" restart.sh"例如):

#!/bin/bash
kill $(pgrep -f 'python3 roomcontrol.py')
python3 roomcontrol.py &

然后致电

os.system('./restart.sh')

在你的" server.py"脚本。

PS:您必须通过运行以下命令使.sh文件可执行:

chmod +x restart.sh

编辑:我不确定如何从不同的shell启动进程,但您可以启动" roomcontrol.py"在另一个终端窗口中使用以下(bash)命令:

gnome-terminal -x sh -c 'python3 roomcontrol.py'

但是你必须更换" restart.sh"由

#!/bin/bash
kill -9 $(pgrep -f 'sh -c python3 roomcontrol.py')
gnome-terminal -x sh -c 'python3 roomcontrol.py'
相关问题