![](/img/trans.png)
[英]Error in executing .jar file from a Python script called from another Python script, as a subprocess
[英]Python Script Hanging After Executing Jar File with Subprocess
似乎无法弄清楚是什么导致脚本挂起,我确定它正在挂起,因为“p2”变量下面的代码似乎根本没有执行。
jar 文件做了我需要它做的事情,用几句话来概括,从某个地方下载所有必要的文件和文件夹。
我需要在删除与其相关的文件之前终止该进程,否则会出现与它被另一个进程占用相关的错误,我相信这是不言自明的。
这是完整的代码,由于原因,路径略有变化:
#!/usr/bin/python3.8
import os
import shutil
import json
import time
import requests
import subprocess
import wget
from zipfile import ZipFile
from glob import glob
def post_to_slack(message):
webhook_url = "someurlhere"
encoded_data = json.dumps({'text': message}).encode('utf-8')
response = requests.post(
webhook_url,
data=encoded_data
)
# print(str(response.status_code))
src = "/somepath/ATM6/"
dest = "/someotherpath/All the Mods 6/"
extension = ".zip"
files_in_dest = os.listdir(dest)
for file in files_in_dest:
filepath = os.path.join(dest, file)
try:
if os.path.isfile(filepath) or os.path.islink(filepath):
os.unlink(filepath)
elif os.path.isdir(filepath):
shutil.rmtree(filepath)
except Exception as e:
post_to_slack(f"Error occured in {os.path.basename(__file__)}!")
for item in os.listdir(src):
abs_path = os.path.join(src, item)
if item.endswith(extension):
file_name = os.path.abspath(abs_path)
zip_ref = ZipFile(file_name)
zip_ref.extractall(src)
zip_ref.close()
time.sleep(20)
contents_in_dir = os.listdir(src)
folder = [content for content in contents_in_dir if content.startswith("SIMPLE") and not content.endswith(extension)]
os.chdir("{}{}".format(src, folder[0]))
wget.download("https://github.com/AllTheMods/alltheservers/releases/download/2.0.1/serverstarter-2.0.1.jar")
p1 = subprocess.run(["chmod", "+x", "serverstarter-2.0.1.jar"], stdout=subprocess.DEVNULL)
p2 = subprocess.Popen(["java", "-jar", "serverstarter-2.0.1.jar", "&"], stdout=subprocess.DEVNULL)
time.sleep(180)
p2.kill()
try:
log_files = glob(src + "**/*.log")
for files in map(str, log_files):
os.remove(files)
zip_files = glob(src + "**/*.zip")
for files in map(str, zip_files):
os.remove(files)
startserver_files = glob(src + "**/startserver.*")
for files in map(str, startserver_files):
os.remove(files)
serverstarter_files = glob(src + "**/serverstarter*.*")
for files in map(str, serverstarter_files):
os.remove(files)
files_to_move = glob(src + "**/*")
for files in map(str, files_to_move):
shutil.move(files, dest)
time.sleep(20)
forge_jar_file = glob(dest + "forge-*.jar")
for files in map(str, forge_jar_file):
print(files)
os.rename(files, "{}{}".format(dest, "atm6.jar"))
except Exception as e:
post_to_slack(f"Error occured in {os.path.basename(__file__)}! {e}")
quit()
这里的这一行: p2 = subprocess.Popen(["java", "-jar", "serverstarter-2.0.1.jar", "&"], stdout=subprocess.DEVNULL)
创建一个 Popen 对象,但它没有触发子进程调用。 所以你的脚本跳转到下一行,这是一个time.sleep(180)
调用; 你给你的“挂”计时了吗? 它的长度可能是 180 秒。 之后,您的其余代码应该会执行。
看看这里的答案; 您可能希望将.wait()
包装在 with 语句中并使用.wait()
命令处理执行。
就像是:
with subprocess.Popen(["java", "-jar", "serverstarter-2.0.1.jar", "&"] as sub:
try:
sub.wait(timeout=timeout)
except:
sub.kill()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.