nitishch
nitishch

Reputation: 130

How to quit terminal after script exits?

I wrote a simple script

#!/bin/bash
nohup $1 &
exit

what it is supposed to do is if I execute 'scriptName' gedit, it must open gedit and then close the terminal. But, problem is terminal is not closed after the script is executed.

Things taken care of:

Upvotes: 1

Views: 7870

Answers (3)

saji159
saji159

Reputation: 328

Given below the sample script.

#!/bin/bash
echo "hi" 
/bin/bash/abc.sh &
sleep 2 
#sleep is used to pause the execution temporary to see the output.
kill -25 $PPID

Value that pass to kill command determines the behavior. Here you can find the different values.

Kill -25 worked for me to close the terminal and make sure to add "&" at the end of command. :)

Upvotes: 1

pobrelkey
pobrelkey

Reputation: 5973

The approach you're trying won't work for the following reason:

The shell for your terminal session is one process; normally when you execute a shell script, the terminal session shell starts a second shell process and runs the script under that. So your exit at the end of the script tells the second shell to terminate - which it would do anyway, as it's reached the end of the script. While you could try killing the first shell process from the second shell process (see comment about $PPID in the other answer), this wouldn't be very good form.

For your script to work the way you expect it to work, you'll need to get your terminal session shell to run the commands in your script by using bash's builtin source command - type source /path/to/your/script gedit, or . /path/to/your/script gedit for short.

Because of the way you'd need to execute this, putting the script on your PATH wouldn't help - but you could create an alias (a "shortcut" that expands to a series of shell commands) to make running the script easier - add a line like alias your_alias_name='. /path/to/your/script' to your ~/.bashrc.

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 207345

I am presuming you are on a Mac - because you mention Preferences. The question is how did you start the script?

If you started the Terminal and then ran "scriptName" from the Terminal, the Terminal won't quit because it is still running your shell. Check the Preferences closely, it says "When the shell exits", not "When the command exits".

Upvotes: 0

Related Questions