0

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 ***********"
ThaDon
  • 7,826
  • 9
  • 52
  • 84
  • 1
    See: [Linux: Prevent a background process from being stopped after closing SSH client](http://stackoverflow.com/q/285015/3776858) – Cyrus Mar 26 '16 at 23:38

1 Answers1

1

You can use

#!/bin/sh
...
nohup [command] &
exit 0
Ren
  • 2,852
  • 2
  • 23
  • 45