Home > front end >  Unable To Kill Jar File Started with Python Subprocess
Unable To Kill Jar File Started with Python Subprocess

Time:11-27

I've got a python script here which downloads a jar file from github, executes it, waits for 3 minutes and is supposed to kill the process.

Note that this works perfectly fine on windows, but somehow the script is not doing what I need it to do on Ubuntu. The jar file indeed does what I need it to do, however after that, the python script does not continue.

To summarise, nothing after p2 = subprocess.Popen(["java", "-jar", "serverstarter-2.0.1.jar", "&"], stdout=subprocess.DEVNULL) gets run. Not even time.sleep(180).

So what I am trying to figure out is why executing the jar file within the script seemingly "stalls" the script.

Note that another python script calls this script in this way subprocess.run(["python3", p, "&"], stdout=subprocess.DEVNULL).

Here is the code:

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

CodePudding user response:

If you on Linux, Type this on terminal:

ps -ef | grep -i java

and check the process ID of your jar file and then kill it using pkill command.

CodePudding user response:

Quote from kill method documentation:

https://pythontic.com/multiprocessing/process/kill

When invoked kill() uses SIGKILL and terminates the process. When SIGKILL is used, the process does not know it has been issued a > SIGKILL. The process cannot do any cleanup. The process is killed immediately.Remember, though miniscule it takes a finite time to kill the process.

Not all process respond to SIGKILL !!!!

Suggesting to kill ALL processes having argument serverstarter-2.0.1.jar in the command.

Like this:

  pkill -9 -f "serverstarter-2.0.1.jar"

If cannot kill ALL processes having argument serverstarter-2.0.1.jar . You need to save the PID of the created process after creation time and kill it.

  • Related