python - 无法终止使用 Python 子进程启动的 Jar 文件

标签 python java python-3.x linux ubuntu

我在这里有一个 python 脚本,它从 github 下载一个 jar 文件,执行它,等待 3 分钟并且应该终止该进程。
请注意,这在 Windows 上工作得很好,但不知何故,该脚本并没有在 Ubuntu 上做我需要它做的事情。 jar 文件确实做了我需要它做的事情,但是在那之后,python 脚本不会继续。
总而言之,p2 = subprocess.Popen(["java", "-jar", "serverstarter-2.0.1.jar", "&"], stdout=subprocess.DEVNULL) 之后没有任何内容。运行。甚至没有time.sleep(180) .
所以我想弄清楚为什么在脚本中执行 jar 文件似乎会“停止”脚本。
注意另一个python脚本以这种方式调用这个脚本subprocess.run(["python3", p, "&"], stdout=subprocess.DEVNULL) .
这是代码:

wget.download("https://github.com/AllTheMods/alltheservers/releases/download/2.0.1/serverstarter-2.0.1.jar", bar=None)

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()

最佳答案

如果您在 Linux 上,请在终端上键入:

ps -ef | grep -i java
并检查 jar 文件的进程 ID,然后使用 pkill 命令将其杀死。

关于python - 无法终止使用 Python 子进程启动的 Jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70104120/

相关文章:

python - 从标记数据中选择多种元素类型的更多 pythonic 方法

python - Python OpenCV 和 MATLAB 之间的不同颜色结果

python - 什么是适用于 Python 2 和 3 的良好的面向命令的命令行框架?

Java无限循环问题

python - 将特定线条添加到 Plotly Scatter3d() 图中

java - 为什么Eclipse调试器会莫名其妙的报错 "Source not found"?

java - 迭代Hashmap时更新不同的项目

python - 我可以在 if 语句中不使用 break 来执行此操作吗?

python - 我收到错误 "Code is unreachable Pylance"这是什么意思,或者我在我的代码中做错了什么?

python - 为什么不需要静态方法装饰器?