Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am new to shell scripting. I am writing a startup script that takes an argument and launches a process based on the argument value.

Example : I want to launch my groovyConsole located inside some directory using the starter script like this.

sh Starter.sh groovy 

Inside the script this is the logic

if [ $1 = "groovy" ] 
then
    cd Works/Groovy/groovy-1.8.6/bin
    ./groovyConsole
fi

This does start the process but I see that this process is bind with the terminal window. If I close the terminal window the process is being killed (obviously). So I want to know if there is a way that running the above command will execute the script and start the process but close the terminal window?

share|improve this question
1  
I wouldn't add the location in the script. Instead, try defining some env variables, like export GROOVY_HOME='Works/Groovy/groovy/' –  santiagobasulto May 7 '12 at 23:09
add comment

4 Answers

up vote 5 down vote accepted

You can detach it from your current process with nohup and &

if [ $1 = "groovy" ] 
then
    cd Works/Groovy/groovy-1.8.6/bin
    nohup ./groovyConsole & 
fi

This would leave the command groovyConsole running until you kill it. This does not close the terminal window though buy you do that like this ...

sh Starter.sh groovy && exit
share|improve this answer
add comment

I am not an expert, how ever this should work.

if [ $1 = "groovy" ] 
then
    Works/Groovy/groovy-1.8.6/bin/groovyConsole &
    exit
fi

Explanation:

  1. No need to CD, just run it directly with absolute path.
  2. & start applications in background
  3. exit to get exit the terminal
share|improve this answer
    
That exit will only exit the shell that is executing as a sub-process of whatever spawned the script. i.e. it won't close the parent shell. –  fluffy May 7 '12 at 18:09
add comment

try change ./groovyConsole to ./groovyConsole & and add an exit after the line

share|improve this answer
add comment

In order for a shell script to close its parent process, it has to kill it explicitly. It is also a very bad practice to do this (because down the road you might be running this script in an environment other than what you're expecting), but if you really want to, you can try:

#!/bin/sh
if [ $1 = "groovy" ] 
then
    cd Works/Groovy/groovy-1.8.6/bin
    nohup ./groovyConsole &
    kill $PPID
fi
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.