Python3重新启动脚本从另一个脚本

问题描述:

情况是这样的:Python3重新启动脚本从另一个脚本

我有2脚本在Python两种不同的炮弹在Linux上运行: 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”壳开始“roomcontrol.py”的新istance,我需要在自己的壳重新启动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: 
+0

我还没有使用子流程,但在文档[Popen](https://docs.python.org/3.6/library/subprocess.html#subprocess.Popen)中快速浏览看起来很有前途。 – Olian04

+0

您是否将shebang添加到脚本roomcontrol.py中? ('#!/ usr/bin/env python') – slallum

+0

@ Olian04我试过了(我编辑过这个帖子) –

创建一个新的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”
+0

嗨,阿列克,谢谢你的回答! 它的工作原理,但我需要在我们遇害的同一个shell中运行脚本。 我不知道我是否自我解释: 我想关闭roomcontrol.py(也许我们也可以关闭这个shell),并在同一个shell中打开一个新的roomcontrol.py(或者如果我们关闭了旧的)。 不是一样的server.py的shell ... –

+0

我编辑了我的答案,但我不确定这是否是你需要的,希望它有帮助,虽然 – Alek

+0

嗨,“来自不同的shell”我的意思是“来自不同的终端窗口“,我的坏! :) 感谢您的接缝工作! –