Calling bash script from python which kills the python process - python

I would like to know whether it possible to launch bash script out of python in another process, whereas in the bash script the is a kill command for the parent python code which gave birth to the bash script?
When the bash script will kill its parent script, will the bash child still be alive?
Thanks

Related

Find Windows PID of a python script with Windows Command Prompt

I am running two different python scripts running on a windows machine simultaneously and would like to kill one but not the other from the command prompt. Using taskkill with the name "python.exe" does not allow me to choose to kill just one of these scripts.
Is there a way in windows to kill just one of these tasks, determined by the script from which it originated?
For example: if I run python_process1.py and python_process2.py and would like to kill the .exe associated with just python_process2.py and leave python_process1.py alone.
UPDATE: the solution below does not kill the process, and the issue still lies in identifying the PID of a process by python script name. If this is impossible, is there a way to selectively kill python scripts on windows that I am unaware of?
Thank you.
Using Get-WmiObject and PowerShell you can examine the command line arguments for each process, then pass the selected Process ID to taskkill.
This example shows how to kill a process executing the script test.py:
PS C:\Users\Administrator> taskkill.exe /F /PID $(Get-WmiObject Win32_Process -Filter "name = 'python.exe'" | Where-Object {$_.CommandLine -like '*.\test.py'} | Select -ExpandProperty ProcessId)
Modify *.\test.py to match how you are actually calling each script, and it should work for you

How to run multiple Python scripts from command line?

I have ten python scripts in the same directory. How to run all of these from command line, that it will work in background?
I use SSH terminal to connect to server CentOS and run Python script as:
python index.py
But when I close client terminal SSH, proccess is died
You can use the & command to make things run in the background, and nohup so it continues on logout, such as
nohup python index.py &
If you want to run multiple things this way, it's probably easiest to just make a script to start them all (with a shell of your choice):
#!/bin/bash
nohup python index1.py &
nohup python index2.py &
...
As long as you don't need to interact with the scripts once they are started (and don't need any stdout printing) this could be pretty easily automated with another python script using the subprocess module:
for script in listofscripts:
#use subprocess.run() for python 3.x (this blocks until each script terminates)
subprocess.call(["python", script], *args) #use popen if you want non - blocking
*args is a link (it's coloring got overwritten by code highliting
also of note: stdout / stderr printing is possible, just more work..

Output freezing when using a python script to run a bash script which runs another script

I have always been able to use Python's subprocess.Popen to run bash scripts without any issues.
However, I am now trying to run a bash script with Popen, and then that bash script is trying to run another script. I did not redirect the output that Popen was getting at all, so all of the output would appear on the terminal. I'm using Ubuntu Linux.
However, when the script that is being called by the bash script finishes, the output on the terminal freezes while the rest of the bash script and the python script continues in the background.
I understand that it might not be the smoothest practice to have a python script run a bash script which also runs a bash script, but I'm hoping to fix this issue. I sense that it is an issue with how I'm running the original bash script inside my python script. Here is the code I'm using:
p = subprocess.Popen(["bash", "myScript.sh", param1, param2, param3])
p.wait()
I originally was using shell=True for the Popen, but that was resulting in the same issue. I also tried removing p.wait() but that also does not resolve the issue.
Any ideas? Should I use a different python method to run the bash script?

Run python script from another python script but not as an child process

Is it possible to run a python script from another python script without wating for termination.
Parent process will terminate immediately after creation of child process.
I tried:
subprocess.Popen([sys.executable, "main.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
and also:
os.system(...)
If you know that the other Python script has a main method, you could simply in you code call that other script:
import main
...
exit(main.main())
But here the other script executes in the context of calling script. If you want to avoid it, you could use the os.exec... functions, by launching a new Python interpretor:
import os
...
os.execl(sys.executable, "python", 'main.py')
The exec family functions will replace (under Unix-Linux) the current Python interpretor with a new one.
You can just add & to start script in background:
import os
os.system('/path/to/script.sh &')
exit()
In this case launched shell script will continue working even after main Python script exits.
But keep in mind that it can cause zombie processes appearance in our system.

os.system('exit') in python

My friend is in a macOS environment and he wanted to call os.system('exit') at the end of his python script to make the terminal close. It doesn't. This doesn't surprise me but I would like to know what exactly is going on between the python script and the terminal when this call is made.
In my mental simulation the terminal should have to tell you that there are still running jobs, but that doesn't happen either.
As a side question : will some less common terminals close when a process calls this?
read the help:
Execute the command (a string) in a subshell.
A subshell is launched, and exit is run in that subshell.
To exit the enclosing terminal, you have to kill the parent. One way to do it is:
os.system("kill -9 %d"%(os.getppid())
The system function starts another shell to execute a command. So in this case your Python scripts starts a shell and runs "exit" command in there, which makes that process exit. However, the Python script itself, including a terminal where it is running, continues to run. If the intent is to kill the terminal, you have to get the parent process ID and send a signal requesting it to stop. That will kill both Python script and a terminal.
Remember that system first spawns/forks a sub-shell to execute its commands. In effect, you are asking only the sub-shell to exit.

Categories

Resources