Welcome!
Important information
-- Spectre and Meltdown vulnerabilities
-- Change in MX sources

News
-- MX Linux on social media: here
-- Mepis support still here

Current releases
-- MX-17.1 Final release info here
-- antiX-17 release info here

New users
-- Please read this first, and don't forget to add system and hardware information to posts!
-- Here are the Forum Rules

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
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

exit terminal when script is done?

#1 Post by GDixon » Thu Jul 24, 2014 1:35 pm

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
Forum Veteran
Forum Veteran
Posts: 22452
Joined: Wed Jul 19, 2006 6:13 am

Re: exit terminal when script is done?

#2 Post by Jerry3904 » Thu Jul 24, 2014 2:48 pm

exit?
Production: 4.15.0-1-amd64, MX-17.1, AMD FX-4130 Quad-Core, GeForce GT 630/PCIe/SSE2, 8 GB, Kingston SSD 120 GB and WesternDigital 1TB
Testing: AAO 722: 4.15.0-1-386. MX-17.1, AMD C-60 APU, 4 GB

User avatar
Adrian
Forum Veteran
Forum Veteran
Posts: 8917
Joined: Wed Jul 12, 2006 1:42 am

Re: exit terminal when script is done?

#3 Post by Adrian » Thu Jul 24, 2014 3:06 pm

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

User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: exit terminal when script is done?

#4 Post by GDixon » Thu Jul 24, 2014 3:16 pm

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
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: exit terminal when script is done?

#5 Post by GDixon » Thu Jul 24, 2014 3:18 pm

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
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: exit terminal when script is done?

#6 Post by GDixon » Thu Jul 24, 2014 4:15 pm

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
Forum Veteran
Forum Veteran
Posts: 8917
Joined: Wed Jul 12, 2006 1:42 am

Re: exit terminal when script is done?

#7 Post by Adrian » Thu Jul 24, 2014 5:13 pm

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
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: exit terminal when script is done?

#8 Post by GDixon » Thu Jul 24, 2014 5:16 pm

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
Forum Veteran
Forum Veteran
Posts: 5919
Joined: Mon Feb 12, 2007 1:45 pm

Re: exit terminal when script is done?

#9 Post by joany » Thu Jul 24, 2014 7:50 pm

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
Forum Veteran
Forum Veteran
Posts: 8917
Joined: Wed Jul 12, 2006 1:42 am

Re: exit terminal when script is done?

#10 Post by Adrian » Thu Jul 24, 2014 8:23 pm

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”