How to properly shutdown process in windows with python? - python

I run ffmpeg from python script and need to shutdown recording on demand.
In Linux I just send SIGTERM. But in Windows as I understand SIGTERM replaced by SIGKILL so records needs to be remixed to play properly.
After googling I found that I should use CTRL_BREAK_EVENT but this signal terminate my parent script too.
What should I use?

Related

How do I send a SIGINT signal to a running process in an iTerm2 session using python?

I'm trying to automate the restarting of several servers running in iTerm2 tabs using iTerm2's Python API. I can get a reference to the sessions, and issue commands to them (with async_send_text), but I can't figure out how to send SIGINT signals to them so I can issue the restart commands.
I've seen a few answers using AppleScript, but it seems like this should be possible in python.
You can use os.kill() to send signals to a process as long as you have the process ID:
import os
import sys
os.kill(<PID>, signal.SIGINT)
This should work even when the process is in running in a terminal emulator.

python bottle zombie process

When developing a bottle webapp with python 3.5, I regularly get a zombie process. I get this when using the auto-restart development mode.
The windows console still updates with the access logs, and the errors, but the program isn't running in foreground anymore, so I can't access it to use Ctrl+C.
The only way to kill this is to open the task manager and end the process manually.
If I don't kill it, it will still be listening on the port, and have precedence on a newly started process.
I haven't found a rule for when this happens, nor have I found a way to reproduce.
How can I avoid this multi-spawned zombie process?

Python handling system shutdown

I have a multiprocessed python application which is being run as an EXE on windows. Upon selecting to shutdown the operating system the applications throws a number of exceptions as a result of the processes being shutdown.
Is there a way to capture the system shutdown request by windows so I may handle the closure of the multiprocesses myself?
A nabble.com page suggests using win32api.SetConsoleCtrlHandler:
“I need to do something when windows shuts down, as when someone presses the power button. I believe this is a window message, WM_QUERYENDSESSION or WM_ENDSESSION. I can't find any way to trap this in python. atexit() does not work. Using the signal module to trap SIGBREAK or SIGTERM does not work either.”
You might be able to use win32api.SetConsoleCtrlHandler and catch the CTRL_SHUTDOWN_EVENT that's sent to the console.
Also see Python windows shutdown events, which says, “When using win32api.setConsoleCtrlHandler() I'm able to receive shutdown/logoff/etc events from Windows, and cleanly shut down my app” etc.

python-twisted and SIGKILL

I have a python application that uses twisted framework.
I make use of value stored in the pidfile generated by twistd. A launcher script checks for it's presence and will not spawn a daemon process if the pidfile already exists.
However, twistd does not remove the .pidfile when it gets SIGKILL signal. That makes the launcher script think that the daemon is already running.
I realize the proper way to stop the daemon would be to use SIGTERM signal, but the problem is that when user who started the daemon logs out, the daemon never gets a SIGTERM signal, so apparently it's killed with SIGKILL. That means once a user logs out, he will never be able to start the daemon again, because the pidfile still exists.
Is there any way I could make that file disappear in such situations?
From the signal(2) man page:
The signals SIGKILL and SIGSTOP cannot be caught or ignored.
So there is no way the process can run any cleanup code in response to that signal. Usually you only use SIGKILL to terminate a process that doesn't exit in response to SIGTERM (which can be caught).
You could change your launcher (or wrap it up in another launcher) and remove the pid file before trying to restart twistd.

How to send control C to Mac Terminal using python?

I have a python script that needs to send control C to the mac terminal. I've tried sending the plain text "^C" but I get back that the terminal does not recognize the command. (The terminal meaning the pseudo terminal that python creates)
Basically, I am using the terminal to run an old Unix Executable and the only way that I can think of to terminate this gracefully is to send the interrupt signal. Is there any way I can fool the terminal into thinking that I pressed control C?
Thanks in advance!
You can explicitly send the SIGINT signal to the process if you can get its PID using os.kill.
os.kill(pid, signal.SIGINT)
This will require you to instrument your script to grab the process PID, but it's the best way to emulate the "ctrl-c" behavior.
If you open the process using subprocess's Popen, you should be able to send a control signal like this:
proc.send_signal(signal.SIGINT)
You'll need to import signal to get SIGINT.

Categories