Page 1 of 2
exit terminal when script is done?
Posted: Thu Jul 24, 2014 1:35 pm
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?
Re: exit terminal when script is done?
Posted: Thu Jul 24, 2014 2:48 pm
by Jerry3904
exit?
Re: exit terminal when script is done?
Posted: Thu Jul 24, 2014 3:06 pm
by Adrian
That works but only if you start the script like this:
. ./scriptname.sh
or
source ./scriptname.sh
Re: exit terminal when script is done?
Posted: Thu Jul 24, 2014 3:16 pm
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
Re: exit terminal when script is done?
Posted: Thu Jul 24, 2014 3:18 pm
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
Re: exit terminal when script is done?
Posted: Thu Jul 24, 2014 4:15 pm
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.
Re: exit terminal when script is done?
Posted: Thu Jul 24, 2014 5:13 pm
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 $$
Re: exit terminal when script is done?
Posted: Thu Jul 24, 2014 5:16 pm
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.
Re: exit terminal when script is done?
Posted: Thu Jul 24, 2014 7:50 pm
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.
Re: exit terminal when script is done?
Posted: Thu Jul 24, 2014 8:23 pm
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.