Can't seem to figure out what is causing the script to hang, I know for sure it is hanging as the code below the "p2" variable doesn't seem to get executed at all.
The jar file does what I need it to do which to summarize in a few words, downloads all the necessary files and folders from somewhere.
I need to kill the process before removing files related to it else there will be an error relating to it being occupied by another process which I am sure it is self explanatory.
Here is the full code with the paths changed a little for reasons:
#!/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()