I have a Python daemon that monitors machine performance during the execution of another software. It basically retrieves data of target processes with ps
and writes it to a CSV file to be plotted when the daemon is stopped.
If the daemon is running in a terminal as a foreground process, the user can stop it with Ctrl+C, what will force a KeyboardInterrupt
exception. I capture that exception and plot the content of the CSVs then.
The problem comes when I have to launch the daemon in a background process with nohup myDaemon.py &
. It works fine, as it generates the CSVs, but as I can't force a KeyboardInterrupt
exception the CSVs are not automatically plotted if I kill or stop the background process with other methods than Ctrl+C.
What I want to avoid is having to move the plotting part to a separate script and run it manually after stopping the daemon.
Found the answer reading kill
man page. It turns out that signal -2 (SIGINT) is the equivalent of Ctrl+C. Tested it running kill -2 <Background_Daemon_PID>
and worked just fine.