Shell Script – How to Bring Background Process to Foreground

job-controlshell-scriptweblogic

As an example:
I have working shell script which starts up weblogic (which will continue to run) and then do deployment

At the end I bring background process back to foreground, so that shell script does not exit (Exited with code 0)

#!/bin/bash

set -m
startWebLogic.sh &

# create resources in weblogic
# deploy war
# ...

jobs -l
fg %1

I had to use set -m, to allow job control, but I also found it is not cleaniest way to use it in non-interactive shells.

Is there a better way to handle it?

Best Answer

As far as I understand your question you are looking for wait:

#!/bin/bash

startWebLogic.sh &

# create resources in weblogic
# deploy war
# ...

wait

It does not "bring the process to foreground". But it does wait until weblogic returns. So the shell script does not exit until weblogic exits. Which is the effect you achieved with fg %1.

from help wait:

Waits for each process identified by an ID, which may be a process ID or a job specification, and reports its termination status. If ID is not given, waits for all currently active child processes, and the return status is zero.

Related Question