exit terminal when script is done?

Here you can exchange scripts that you created or have permission to share with other users.
Message
Author
User avatar
GDixon
Posts: 51
Joined: Fri Nov 02, 2007 4:39 pm

exit terminal when script is done?

#1 Post by GDixon »

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?

User avatar
Jerry3904
Administrator
Posts: 21881
Joined: Wed Jul 19, 2006 6:13 am

Re: exit terminal when script is done?

#2 Post by Jerry3904 »

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

User avatar
Adrian
Developer
Posts: 8248
Joined: Wed Jul 12, 2006 1:42 am

Re: exit terminal when script is done?

#3 Post by Adrian »

That works but only if you start the script like this:
. ./scriptname.sh
or
source ./scriptname.sh

User avatar
GDixon
Posts: 51
Joined: Fri Nov 02, 2007 4:39 pm

Re: exit terminal when script is done?

#4 Post by GDixon »

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

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

User avatar
GDixon
Posts: 51
Joined: Fri Nov 02, 2007 4:39 pm

Re: exit terminal when script is done?

#5 Post by GDixon »

Adrian,

I put 99% of my scripts in /usr/local/bin so they are in my path and they normally are script_name.sh

User avatar
GDixon
Posts: 51
Joined: Fri Nov 02, 2007 4:39 pm

Re: exit terminal when script is done?

#6 Post by GDixon »

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.

User avatar
Adrian
Developer
Posts: 8248
Joined: Wed Jul 12, 2006 1:42 am

Re: exit terminal when script is done?

#7 Post by Adrian »

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

Code: Select all

pid=`ps -p $PPID -o ppid=`
kill $pid
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 $$
Last edited by Adrian on Thu Jul 24, 2014 5:20 pm, edited 1 time in total.

User avatar
GDixon
Posts: 51
Joined: Fri Nov 02, 2007 4:39 pm

Re: exit terminal when script is done?

#8 Post by GDixon »

But if I kill my Grandparent will I still be born :)

Time to go fishing and think a bit less. Tomorrow is another day.

User avatar
joany
Posts: 235
Joined: Mon Feb 12, 2007 1:45 pm

Re: exit terminal when script is done?

#9 Post by joany »

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.

User avatar
Adrian
Developer
Posts: 8248
Joined: Wed Jul 12, 2006 1:42 am

Re: exit terminal when script is done?

#10 Post by Adrian »

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.

Post Reply

Return to “Scripts”