I am in the process of creating a Python program (lets call it Program1), which will terminate randomly. I need another program to check if Program1 has been terminated, and if so, to restart it after a given amount of time. Should I use another Python program to do this, or is there a way this can be done in Windows e.g. similar to a cron job?
From what I understand, you can use sys.exit(), exit() and quit() to kill a Python program, but do all of these also kill all other running Python programs?
Thanks
Reliably determining whether a specific program is still running or not can be done in multiple ways.
A good way to do so would be using a specific lock file. You can find how to do this in Python described in this thread.
To answer the second part of your question: sys.exit(), exit() and others will only stop the currently running Python process, not any others that might be running concurrently.
Related
Suppose that I run an MPI program involving 25 processes on 25 different machines. The program is initiated at one of them called the "master" with a command like
mpirun -n 25 --hostfile myhostfile.txt python helloworld.py
This is executed on Linux with some bash script and it uses mpi4py. Sometimes, in the middle of execution, I want to stop the program in all machines. I don't care if this is done graciously or not since the data I might need is already saved.
Usually, I press Ctrl + C on terminal of the "master" and I think it works as described above. Is this true? In other words, will it stop this specific MPI program in all machines?
Another method I tried is to get the PID of the process in the "master" and kill it. I am not sure about this either.
Do the above methods work as described? If no, what else do you suggest? Note that I want to avoid the use of MPI calls for that purpose like MPI_Abort that some other discussions here and here suggest.
I try to run a python program on my mac terminal. I want to manually terminate it by pressing "cmd + z". I openup "monitor", and I still see python-2.7 getting 8GB ram (wtf?), and using some cpu resources. How do I completely shut down this python program (I can't force quit because I also got another python program running on a different and separate terminal.
Just want to clarify my description, I ran two python programs, and in the "monitor", I see two Python-2.7 running.
I want to terminate one of the python program running in my terminal. How do I completely shut down that one (and also free up the resources that it is using).
cmd + Z puts the currently-running process into the background. Ctrl+D will cause Python to exit
You can use kill with the pid of your background process to close it
You can check for the PIDS for any jobs with:
lsof +D path/folder
and then:
kill -9 PID-number
Also, ctrl+Z while the process is running can stop it as well.
You can quit a python program by typing quit() without the quotes of course. Be sure to include the parentheses.
This question extends/revives this one.
The relevance to revive this topic is due to the failure in solving the same problem with the given answers.
The bash script executes a python script embedded. Something like
#!/bin/bash
./pyscript.py
chmod +x pyscript.py permission was given.
Alternative ways to run the script were used.
(python -u pyscript.py or /usr/bin/python pyscript.py)
As the title states the python program does not exit.
I have tried the following attempts within the python script to solve the issue:
sys.exit(0); %the program catches the correct exception
os._exit(1) %does not work and the correct exception is catched
sys.stdout.flush() %to clean the buffer of the stdout
The daemon solution is not suitable for what I need, because running in the background independently from the main script will not wait for the execution of the python program untill the end.
What are the alternative solutions that remain for this case?
Have you tried to use strace -p $PID on the python process? The output will not always be useful however.
From the code perspective, in addition to threads I would check if there are any signal handlers (which maybe do not terminate for some reason).
As far as threads are concerned, you might be interested in this, although I believe someone mentioned it in the other thread.
Finnally the problem is solved.
The program in python wich I've been trying to kill the process runs with multiple threads.
sys.exit(0) only terminates the thread in which the program is called.
The os._exit(1) was called with the sys.exit(0) before its execution (fail!).
By running os._exit(1) without sys.exit(0) before, the program exit the python script.
The reason must be that sys.exit() only terminates the thread in which it is called and allows the program to clean resources, while os._exit() does an abrupt program termination.
Found here.
With this solution it's better guarantee the termination of any task the program should end and then call os._exit.
what I usually do to separate a script from the main shell terminal process is sending the script inside a screen session detached. Then I kill the pid of the script without any trouble.
But for this particular case I want the program waiting for the end of the python subscript and not as a parallel process.
Also, you might want to try the trace module, i.e. running your program with #!/usr/bin/env python -m trace --trace. If python is executing some of your code (which it probably is), it should show you details on that.
I thought this is an easy task but I googled around and had no clue at all.
I'm trying to run the pyhook tutorial and once I get to pythoncom.PumpMessages(), the function will stay idle and wait for Windows events.
I don't want to close down the window nor restart the shell because of all imported library and defined functions. Is there any way to termindate just the function?
I tried ctrl+c but it doesn't work at all.
Thanks a lot.
I would like to write a python script that will finally be converted to .exe (with pyinstaller lets say) and added to windows startup applications list. This program (once launched) should 'monitor' user and wait until user will start a specified program (say program1.exe) and then my python program should launch another specified program (program2.exe).
I know that there is something like subprocess to launch another program from python script but I was not able to make it work so far ;/ And as it comes to this part where I need to monitor if user does start specified program I have no idea haw to bite on this. I don't expect a complete solution (although it would be very nice to find such ;p) but any guides or clues would be very helpfull.
For the monitoring whether or not the user has launched the program, I would use psutil: https://pypi.python.org/pypi/psutil
and for launching another program from a python script, I would use subprocess.
To launch something with subprocess you can do something like this:
PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
subprocess.call([PATH_TO_MY_EXTERNAL_PROGRAM])
If it is as simple as calling an exe though, you could just use:
PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
os.system(PATH_TO_MY_EXTERNAL_PROGRAM)
Hope this helps.
-Alex