How to start a subprocess, that is exited when my main process exits


I have a simple Go application, which occasionally runs bash scripts in the background.

For simplicity, I am just starting chrome with a webpage in the background.

#!/bin/env bash


Now 2 things can happen:

  1. There is already a chrome window open. In this case, chrome opens the URL in the existing window and exits.
  2. There is not already a chrome window open. In this case, the chrome command becomes the chrome main process and is bound to the browser window. This means the process does not exit if I do not close the browser window and the browser exits if my Go application exits (because it’s a child process).

I do not want the second case to happen! Because in that case, if I quit the application chrome also quits and I do not want that.

Can I somehow in Go start a process (with something like exec.Command) and completely disconnect it from my application, so that the process runs independently (does not exit with my process, does not block it)?


This seems to work for me:

package main
import "os/exec"

func main() {

To respond to a comment. Regarding Wait, if you call Wait, it’s essentially the same thing as calling Run. The point of Wait is it allows for you to put code between the start of the command, and when you are waiting for it to complete.

Contrast that with Run, where the "Start" and "Wait" happen one right after the other, without the option to put anything in between. Or in this case, where you don’t want the Wait at all.

Answered By – Zombo

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.