I'm new to Linux. I have python scripts in different files, each one performing a desired function.
foo1.py foo2.py foo3.py
Each one of these scripts should do cleanup before being terminated. Moreover, I would like to execute each one of them in a terminal to interact and read the outputs separately.
I already implemented the cleanup function to catch different kill signals (e.g., SIGTERM, SIGINT, etc.).
To run each one of the scripts in a terminal, I am using a
startup.sh that opens one
xterm terminal for each, just like below.
xterm -e python foo1.py & pid1=$! xterm -e python foo2.py & pid2=$! xterm -e python foo3.py & pid3=$!
Then, in my logic, the script
foo3.py is the most important, so if it finishes, the other two need to close as well.
wait $pid3 kill -15 $pid1 kill -15 $pid2
The problem is that
$pid2 are pids for the xterms and not for the scripts (
foo.py) themselves. So, when I kill the xterms (
kill -15 $pid), the python scripts have no chance to execute their cleanup routine.
I tried to catch the signal sent by xterm to its child process (https://unix.stackexchange.com/a/54994/357513) but the python scripts are immediately finished.
So, Is there a way to get the pids from the scripts running inside xterm? Or Is there another way to make sure that such cleanup routines will be executed?