Python Forum
How to run a particular file in subprocess
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to run a particular file in subprocess
#1
I am trying to check if the process is running by giving the process name and if there i am trying to kill and i want to restart the process again. So inside subprocess.call i give the path for the file i need to call. My question here is inside the (/examples) i have many executable files like example1.py,exmaple2.py etc. When i run this script now it shows process found and then kills it. I need to know how i can pass the particular example that i want to call again from the terminal(like i want to execute example1.py and later another script)

for process in psutil.process_iter():
if (process.name().startswith('python')) and ("new_scene" in process.cmdline()[1]):
    print('Process found')
    process.kill()
    time.sleep(3)
    subprocess.call('python /Desktop/Test/Script/new/new_scene.py --run  /Desktop/Test/Script/new/scenario/examples', shell=True)
    break
else:
print('Process not found')
Reply
#2
(Nov-23-2021, 02:45 PM)Shiri Wrote: I am trying to check if the process is running by giving the process name and if there i am trying to kill and i want to restart the process again. So inside subprocess.call i give the path for the file i need to call. My question here is inside the (/examples) i have many executable files like example1.py,exmaple2.py etc. When i run this script now it shows process found and then kills it. I need to know how i can pass the particular example that i want to call again from the terminal(like i want to execute example1.py and later another script)

for process in psutil.process_iter():
if (process.name().startswith('python')) and ("new_scene" in process.cmdline()[1]):
    print('Process found')
    process.kill()
    time.sleep(3)
    subprocess.call('python /Desktop/Test/Script/new/new_scene.py --run  /Desktop/Test/Script/new/scenario/examples', shell=True)
    break
else:
print('Process not found')

It sounds to me like you are looking for the module sys which is used to pass arguments to scripts/programs

import sys

print(sys.argv[0])
print(sys.argv[1])
Reply
#3
I tried to add something like this for above script .Does this make sense to call sys.argv inside subprocess.call

time.sleep(5)
cmd='python /Desktop/Test/Script/new/new_scene.py --run  /Desktop/Test/Script/new/scenario/examples'
subprocess.call(cmd + sys.argv[1],shell=True)
break
Reply
#4
(Nov-24-2021, 08:42 AM)Shiri Wrote: I tried to add something like this for above script .Does this make sense to call sys.argv inside subprocess.call

time.sleep(5)
cmd='python /Desktop/Test/Script/new/new_scene.py --run  /Desktop/Test/Script/new/scenario/examples'
subprocess.call(cmd + sys.argv[1],shell=True)
break

It looks okay to me but you need to format the command proper. It should probably be.

cmd='python /Desktop/Test/Script/new/new_scene.py --run  /Desktop/Test/Script/new/scenario/'
Then you can append the command file at the end like you're doing.
Reply
#5
Thank you on the feedback, a quick question how can I run multiple process (want to run simultaneously by passing arguments) using the subprocess.call ?
Reply
#6
(Nov-24-2021, 09:12 AM)Shiri Wrote: Thank you on the feedback, a quick question how can I run multiple process (want to run simultaneously by passing arguments) using the subprocess.call ?

You might want to take a look at this thread threads and subprocess.call
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 491 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  Subprocess.Popen() not working when reading file path from csv file herwin 13 15,068 May-07-2021, 03:26 PM
Last Post: herwin
  subprocess call cannot find the file specified RRR 6 16,606 Oct-15-2020, 11:29 AM
Last Post: RRR
  subprocess error : The system cannot find the file specified evilcode1 0 10,706 Oct-03-2018, 08:04 AM
Last Post: evilcode1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020