0

I need to write a bash script that starts a blocking process, then detaches from it so I can run some other commands that configure this running process, then reattach to the original process such that ctrl+c will kill it.

Specifically, this happens to be the Google Cloud PubSub emulator, so the bash script looks something like the following pseudocode:

# TODO: pubsub will block, but I need to detach from it so I can create topics.
gcloud beta emulators pubsub start --host-port=localhost:8086

python publisher.py myapp create topic1
python publisher.py myapp create topic2

# TODO: Now I need to reattach to the pubsub process.

jacob
  • 2,762
  • 1
  • 20
  • 49
  • 1
    You can do this using `screen` or `tmux`, by running the command that you want to detach from in a virtual terminal session. – Barmar Jul 11 '19 at 16:28
  • If you can add an answer with the actual bash script code, and ideally without having to install any additional software, I'll mark it as the answer. – jacob Jul 11 '19 at 16:31
  • 1
    I'm not sure of the exact code required, I've never scripted these tools, I just use them interactively. – Barmar Jul 11 '19 at 16:37
  • 1
    FYI, `screen` comes with MacOS. – Barmar Jul 11 '19 at 16:39
  • @Barmar Thanks! I got it working with screen. I'm going to post an answer, since you didn't :‑P – jacob Jul 11 '19 at 17:02
  • Glad I could push you in the right direction. – Barmar Jul 11 '19 at 17:22
  • Possible duplicate of [nohup vs screen — which is better for long running process?](https://stackoverflow.com/q/20766300/608639), [How to make a programme continue to run after log out from ssh?](https://stackoverflow.com/q/954302/608639), [In Linux, how to prevent a background process from being stopped after closing SSH client](https://stackoverflow.com/q/285015/608639), etc. – jww Jul 11 '19 at 23:01

1 Answers1

2

Thanks to @Barmar's comments above, I have a working solution on MacOS:

screen -S pub_sub_emulator -dm gcloud beta emulators pubsub start --host-port=localhost:8086

python publisher.py myapp create topic1
python publisher.py myapp create topic2

screen -r pub_sub_emulator
jacob
  • 2,762
  • 1
  • 20
  • 49