( Updated ) I think I understand what you are looking for now:
You can accomplish this by creating a new terminal to run the script:
gnome-terminal -x /path_to_dir_of_your_script/your_script_name
(or use xterm -e or konsole -e instead of gnome-terminal -x , depending on which system you are on)
So, whenever your script ends / terminates (I assume that you have exit 0 or exit 1 in some parts of the script), the newly created terminal will also exit with the completion of the script, which in turn will also destroy any applications created in this new terminal.
For example, I just tested the above command with this script:
#!/bin/bash gedit & pid=$! echo "$pid" sleep 5 exit 0
As you can see, there are no explicit calls to kill the new gedit process, but the application (gedit) closes as soon as the script exits.
(Previous answer: alternatively, if you were just asking how to kill the process) Here is a brief example of how you can do this with kill .
#!/bin/bash gedit & pid=$! echo "$pid" sleep 5 kill -s SIGKILL $pid
If I do not understand your question, you can immediately receive a PID response process, and not wait until it ends.
source share