0

I'm writing script which will be run from removable media but requires this media to be unmount during execution. It can't just do it straght because medium is busy then. Thus I've split script into 2 parts - one running before unmount which is copying second script part to ramfs and launching it, second part which is unmounting madia, doing job then self-deleting and unmounting creted ramfs. But the problem is that asynchronous script started in bash launches in foreground and I don't know how this script could bring itself programmatically foreground to get user input.

So i need something like this:

script1 does his job
script1 starts script2
script1 dies
script2 goes to foreground
script2 unmounts media with script1
script2 does his job
scirpt2 starting async command and dies
async command unmounts ramfs
Lapsio
  • 6,384
  • 4
  • 20
  • 26

1 Answers1

1

You want wait:

script1 &
JOB_SCRIPT1=$!
script2 &
JOB_SCRIPT2=$!
wait $JOB_SCRIPT1
echo "this action takes place after script1 but perhaps during script2"
wait $JOB_SCRIPT2
echo "now both jobs are complete"
async command unmounts ramfs

However, this doesn't seem to do exactly what you want; it appears that there are some background jobs spun up by script1. That's harder to control for. Perhaps try wait without arguments and see if that picks up the fact that there are backgrounded processes (but I'm dubious since they're not a part of the same shell):

script1 &
wait
script2
async command unmounts ramfs

(The ampersand in this version shouldn't actually do anything, but may cue the shell into what you want. You'll have to try it out to see.)

Adam Katz
  • 14,455
  • 5
  • 68
  • 83
  • but it's hard to control. I mean I'd need third script to launch this thing but then it'd need to be on sdcard and it wouldn't work either. I want to literally type in shell: `$ /drive/script.sh` and enter execution thread which will umount /drive and do few other things, prompting questions. I've actually managed to start script1, then start script2 after script1 dies, but script2 doesn't run as foreground process then. I've tried to run `(sleep 1 && script2) & ; exit 0 && fg` from script1 but it throws I/O error when script2 calls `read` – Lapsio Jul 30 '15 at 15:43