Nivek Defend Nivek Defend - 1 month ago 18
Bash Question

Close terminal window after using xterm

I have the following code

xterm -e <some command> &
<line 2 command>


Now after the line 2 command I want the close the xterm -e window automated.

How can I close the terminal window that I opened in the my script?

Thanks!

Answer

You can simply get PID of the last background task lauched with $!, and then kill it after your line 2 command is achived.

xterm -e <some command>  &
myBackgroundXtermPID=$!
<line 2 command>
kill $myBackgroundXtermPID

I assume that if your background process ends before line 2 , its PID will not be reused by another process (if you run out of PID numbers).

Comments