ThaDon
ThaDon

Reputation: 8068

Exiting a bash shell after script backgrounds a task

I have the following script that I run from a bash shell. After I execute it and I observe the ********* Exiting *********** statement I wish to quit the bash shell but have the backgrounded java process continue. However, when I do type exit at my bash prompt it simply echo's back exit but then just sits there.

Question: is there a way for me to background a task from within a script such that I can exit the shell and have the task continue?

#!/bin/sh
pid=$(cat /tmp/myapp.pid)
if [ "$(ps -o comm= -p "$pid")" = "java" ]; then
  echo "Killing the previous myapp process"
  kill $pid
else echo "Could not find a previously running myapp process"
fi
cd ~/deploy
echo "Launching myapp"
java -Dconfig.file=./myapp-1.conf -jar myapp.jar & echo "$!" > /tmp/myapp.pid
sleep 5
echo "********* Exiting ***********"

Upvotes: 0

Views: 193

Answers (1)

Ren
Ren

Reputation: 2946

You can use

#!/bin/sh
...
nohup [command] &
exit 0

Upvotes: 1

Related Questions