Close cmd without interrupting python script - python

I have a python script that needs to be run from cmd, but should also stay running once the cmd window has been closed. I've tried various combinations of START and CALL but I've hit a mental blank (and google isn't helping at all).
Here's what I have:
#echo off
start /b python server.py
start python client-sendmessage.py
exit
server.py is a singleton, and should continue running after exit is reached. client-sendmessage.py should halt the script until it returns.

Related

Auto rerun of python scripts

seeking everyone's help on my workplace python processes
Context:
-I have some python scripts running 24/7 in Powershell to process files that it detects in my folder), and then store them in the database (MySQL).
-However sometimes the script would hang (eg. due to a new error encountered, work server crash, etc).
-It caused some downtime as only my maintenance team can reset the script and they are not always around.
Trying to do:
-Whenever it detects that the script hangs, it will auto restart the scripts.
Apologies that I do not have sample scripts as all of them are stored in my workplace. I would appreciate it if you can share with me the rough thought process or examples (on other sites) so I can 'take off' on my own. thank you :)
The following bash script might help you:
#!/bin/bash
scripts=('python3 script1.py' 'python3 script2.py' 'python3 script3.py')
# loop through each python script you want and execute
for s in "${scripts[#]}"; do
eval $s # run the script
# pick up the exit code in variabl
python_exit_status=$?
if [ "${python_exit_status}" -ne 0 ];
then
echo "Exit code not equal 0. Python script errored ... "
# here, you want to reset from the beginning. Break and rerun entire bash script?
else
echo "Exit code 0. Continue as normal ... "
# here, do nothing and allow it to continue.
fi
done
The concept is that you loop through each of your python scripts, pick up on the exit code of said script, and then have a region where the bash code goes when the exit code is not equal to zero (i.e. Python script error/hangup occurs). Not sure what you want to do there though so I did not add any more. Did you want to restart from script1 again if script2 hangs up?

How to run python script in linux terminal console and continue using command line?

My question seems to be quite easy, but for some reason I did not find a quick answer to it. I have a python script that I want to run on the terminal command line (Ubuntu linux server), which works for me. But then I can't use the command line until the script ends. The script takes a long time to run, and I would like to continue using the command line to perform other tasks. How can you do the work of a script when its progress is not shown on the command line, but keep its work? And how can I see the active processes that are running on the server to see if a process is running?
Run script command:
python script.py
Add next with & echo "123":
The script takes a long time to run, and I would like to continue
using the command line to perform other tasks.
It seems that you want to run said process in background, please try pasting following
python script.py &
echo "123"
it should start your script.py and then output 123 (without waiting until script.py ends)
how can I see the active processes that are running on the server to
see if a process is running?
Using ps command
ps -ef
will list all processes which you would probably want to filter to get interested one to you.

Python and Win10 - run command and repeat when finished

I am currently using os.system('start cmd.exe /k ' + command)
where “command” is a string that opens an exe file with some arguments.
I know I could avoid calling cmd.exe, but to me it’s easier with that.
When the executable stops, cmd doesn’t close and remains idle waiting for another command; what I want to do is: when the execution finishes, make my python script call the same command again in the same cmd window.
EDIT: I am running multiple instances of cmd.exe which open the same exe with different parameters, my aim is to do the same as above for every instance
returncode = os.system(mycommand)
print(returncode)
prints “0” just after running cmd (without waiting for the execution to end)
I don’t really need the exit code, but parsing the text output from the window might be useful to know whether to stop looping the command or just wait some time before running it again

what shall I do to kill a running script and go back to the prompt in python

I use Ctrl+C each time to kill a running script and go back to the prompt. But then I lose the prompt, only to get "KeyboardInterrupt".
How should I kill a running script and get the python prompt back?
If you're saying that you want to launch the Python interactive terminal after a script finished running (both normally and by keyboard interrupt), then just launch Python with the -i tag. For example: python -i <script-name> <script-args>

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