首页 > 解决方案 > 如何制作与进程分离然后重新附加到它的bash脚本?

问题描述

我需要编写一个 bash 脚本来启动一个阻塞进程,然后从中分离,这样我就可以运行一些其他命令来配置这个正在运行的进程,然后重新附加到原始进程,这样 ctrl+c 就会杀死它。

具体来说,这恰好是 Google Cloud PubSub 模拟器,因此 bash 脚本类似于以下伪代码:

# 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.

标签: linuxbashmacossh

解决方案


感谢@Barmar 上面的评论,我在 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

推荐阅读