exit terminal when script is done?
exit terminal when script is done?
When you have a script that runs in a termial and finishes with no errors does what it was meant to do how would you end it so that when it is finished it closes the terminal also?
Re: exit terminal when script is done?
exit?
Production: 5.10, MX-23 Xfce, AMD FX-4130 Quad-Core, GeForce GT 630/PCIe/SSE2, 16 GB, SSD 120 GB, Data 1TB
Personal: Lenovo X1 Carbon with MX-23 Fluxbox and Windows 10
Other: Raspberry Pi 5 with MX-23 Xfce Raspberry Pi Respin
Personal: Lenovo X1 Carbon with MX-23 Fluxbox and Windows 10
Other: Raspberry Pi 5 with MX-23 Xfce Raspberry Pi Respin
Re: exit terminal when script is done?
That works but only if you start the script like this:
. ./scriptname.sh
or
source ./scriptname.sh
. ./scriptname.sh
or
source ./scriptname.sh
Re: exit terminal when script is done?
any kind of exit such as exit, exit 0, exit 1 just sends the script into a loop.
what i'm looking to do is have the terminal close after using q to quit the wol.sh script i posted in scripts.
Heres the ending of that script
what i'm looking to do is have the terminal close after using q to quit the wol.sh script i posted in scripts.
Heres the ending of that script
Code: Select all
if [ $input1 == Q ] || [ $input1 == q ]; then
/usr/bin/clear
echo
echo -e "later gator!\n"
sleep 2; /usr/bin/clear # use sleep (in seconds) to adjust time Later gator is on screen
exit 1
fi
done
Re: exit terminal when script is done?
Adrian,
I put 99% of my scripts in /usr/local/bin so they are in my path and they normally are script_name.sh
I put 99% of my scripts in /usr/local/bin so they are in my path and they normally are script_name.sh
Re: exit terminal when script is done?
ok using exit of any kind will exit the shell but not the terminal.
Per Adrians suggestion I have found that starting the script in terminal like this
(dot) (space) script_name.sh will run the script in a shell in the terminal. when the script finishes the exit commands will exit the shell but not the terminal. The leading (dot) . with a (space) will close the terminal when the script exits the shell.
Thanks to Adrian for pointing me in a direction that works.
ok I have this down now to find a way to do it from within the script so you don't need to type the leading dot with a space.
Per Adrians suggestion I have found that starting the script in terminal like this
(dot) (space) script_name.sh will run the script in a shell in the terminal. when the script finishes the exit commands will exit the shell but not the terminal. The leading (dot) . with a (space) will close the terminal when the script exits the shell.
Thanks to Adrian for pointing me in a direction that works.
ok I have this down now to find a way to do it from within the script so you don't need to type the leading dot with a space.
Re: exit terminal when script is done?
You could kill the grandparent process, but that's normally not recommended because you might not always call it from the same place, but in any case here's the code
Explanation (why grandparent)
terminal = grandparent process, obtain PID with ps -p $PPID -o ppid=
bash = parent process, obtain PID with $PPID
script = current process, obtain PID with $$
Code: Select all
pid=`ps -p $PPID -o ppid=`
kill $pid
terminal = grandparent process, obtain PID with ps -p $PPID -o ppid=
bash = parent process, obtain PID with $PPID
script = current process, obtain PID with $$
Last edited by Adrian on Thu Jul 24, 2014 5:20 pm, edited 1 time in total.
Re: exit terminal when script is done?
But if I kill my Grandparent will I still be born :)
Time to go fishing and think a bit less. Tomorrow is another day.
Time to go fishing and think a bit less. Tomorrow is another day.
Re: exit terminal when script is done?
I think the problem is that the shell script cannot stop the parent process (the terminal), although the terminal can certainly stop the child process. As Adrian said, you have to kill the parent (terminal) from a higher process.
MX-14; 3.12-0.bpo.1-686-pae kernel using 4GB RAM
2.4GHz AMD Athlon 4600+
NVidia GeForce 6150 LE; 304.121 Display Driver
You didn't slow down because you're old; you're old because you slowed down.
2.4GHz AMD Athlon 4600+
NVidia GeForce 6150 LE; 304.121 Display Driver
You didn't slow down because you're old; you're old because you slowed down.
Re: exit terminal when script is done?
You can kill the parent from the children, that's not a problem, just make sure you kill the right parent. If you are sure the script is started the same way each time then what I posted above should work fine.