I am typing a small bash script that should clone the git repository, check for a specific hard-bound branch, and listen for some new commits. If new commits are found, the script should kill the executable instance of "MyApp", do a git pull, and finally build it using gradle. It should also start with gradle again.
Yes, this is a Java application with a gradle build file.
This is my code:
##!/bin/bash # GitHub Repository and Paths GLOBAL_REPOSITORY="..." LOCAL_REPOSITORY="..." # Go to the repository cd $LOCAL_REPOSITORY # Clone the git repository if not already done if [ ! -d "$LOCAL_REPOSITORY/.git" ] then git clone $GLOBAL_REPOSITORY $LOCAL_REPOSITORY git checkout dev fi # Pull and build if there are new commits LAST_GLOBAL_COMMIT="" LAST_LOCAL_COMMIT="" CHILDS_PID="" while true; do git fetch origin LAST_GLOBAL_COMMIT="$(git rev-parse origin/dev)" LAST_LOCAL_COMMIT="$(git rev-parse dev)" if [ "$LAST_GLOBAL_COMMIT" != "$LAST_LOCAL_COMMIT" ] then if [ "$CHILDS_PID != "" ] then kill 9 "$CHILDS_PID" # Line 33 fi LAST_LOCAL_COMMIT="$LAST_GLOBAL_COMMIT" git pull origin dev gradle CHILDS_PID="$(gnome-terminal -e \"gradle run\" & echo $!)" fi sleep 300 done
There is a problem that the child terminal and MyApp are still working. It seems only gradle dies. But I want to kill all the child processes, and then start them again. Only the terminal that runs my script should not be killed.
In a real situation, the process tree should look like this:
Main Terminal (Running the script) | | Child Terminal | | Gradle run | | MyApplication
My question is: how can I kill all these processes at a time, except for the terminal at the top of line 33?
EDIT:
The difference here is that I was looking for a way to find the main node of the subtree and kill it in the second step.
But I found out that the bash instance calls the gnome terminal or any other terminal or tty, it creates a process that initializes the forked terminal.
So, for example, terminal_1 has PID code 42, and I start another terminal through it, then it will start the initialization process with PID code of 43, which initializes terminal_2, which will have PID code 44. But terminal_1 only gets 43 in as the return value. After that, the init process dies, and if you try to kill PID 43, it logically says that there is no process with this PID.
Tracking is not possible at this point, and I solved the problem by writing a c program that uses fork on its own.