Python Forum

Full Version: How to run same process simultaneously
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a jar and I want to make a python script for linux that checks if the jar is opened in the linux task manager and if its not to open it 5 times. From time to time it should check the 5 processes and when all 5 end open them again.

This is the code that I`ve written in python:

import psutil

import subprocess

process_name = "/root/Linux/ArhivareProd.jar"

for proc in psutil.process_iter():

process = psutil.Process(proc.pid)

pname = process.name()

if pname != process_name:

subprocess.call(['java','-jar','/root/Linux/ArhivareProd.jar'])

subprocess.call(['java','-jar','/root/Linux/ArhivareProd.jar'])

subprocess.call(['java','-jar','/root/Linux/ArhivareProd.jar'])

subprocess.call(['java','-jar','/root/Linux/ArhivareProd.jar'])

subprocess.call(['java','-jar','/root/Linux/ArhivareProd.jar'])


else:

print("Minim un proces ArhivareProd.jar deja ruleaza !")
When I run it it only opens one jar process

How can I modify the script so it opens up 5 processes ?

Thanks
Try
procs = []
for i in range(5):
    p = subprocess.Popen(['java','-jar','/root/Linux/ArhivareProd.jar'])
    procs.append(p)
from p in procs:
    p.wait()