FAQ
Hi

I've been spending my day looking for a way to start child processes from a
Go program (the parent) such that, if the parent process dies, the children
doesn't. I tried using 'nohup' as a go-between, but can't seem to start the
child processes in the background. Are there any recommended (or
reasonable) work-arounds for this?

Thanks,
:) Hein

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Shawn Milochik at Jul 18, 2014 at 1:15 am
    It looks like exec.Start() does what you want.

    http://golang.org/pkg/os/exec/#Cmd.Start

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Hein Meling at Jul 18, 2014 at 1:56 am
    No, because if the go program (the parent) is killed, the children also gets killed.

    Hein :)

    Sent from my iPad
    On 17 Jul 2014, at 18:15, Shawn Milochik wrote:

    It looks like exec.Start() does what you want.

    http://golang.org/pkg/os/exec/#Cmd.Start


    --
    You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/shST-SDqIp4/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Jul 18, 2014 at 2:04 am

    On Thu, Jul 17, 2014 at 9:56 PM, Hein Meling wrote:

    No, because if the go program (the parent) is killed, the children also
    gets killed.

    Hein :)

    That doesn't seem to be the case.

    package main

    import (
         "os/exec"
    )

    func main() {
         cmd := exec.Command("/tmp/monkey.sh")
         cmd.Start()
    }

    When I run that, with monkey.sh being a program that just appends text to
    a file, it keeps running after the Go program exits. I can even run the Go
    program multiple times and multiple instances of monkey.sh are running.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Hein Meling at Jul 18, 2014 at 3:12 am
    Your solution seems to work when then parent process exits cleanly. But if
    you do:

    func main() {
    cmd := exec.Command("sleep", "15")
    cmd.Start()
    time.Sleep(20 * time.Second)
    }

    And CTRL-C the parent process before it exits cleanly, then also the sleep
    process goes away. Does the same happen to your monkey.sh process?

    Moreover, my use case is actually somewhat more involved. That is, I would
    like to do cmd.Wait() (from different goroutines), in order to monitor the
    child processes, but I don't want the child processes to fail if the parent
    fails.

    :) Hein

    On Thursday, July 17, 2014 7:04:25 PM UTC-7, Shawn Milochik wrote:

    On Thu, Jul 17, 2014 at 9:56 PM, Hein Meling <hein....@gmail.com
    <javascript:>> wrote:
    No, because if the go program (the parent) is killed, the children also
    gets killed.

    Hein :)

    That doesn't seem to be the case.

    package main

    import (
    "os/exec"
    )

    func main() {
    cmd := exec.Command("/tmp/monkey.sh")
    cmd.Start()
    }

    When I run that, with monkey.sh being a program that just appends text
    to a file, it keeps running after the Go program exits. I can even run the
    Go program multiple times and multiple instances of monkey.sh are running.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Bryan Whitehead at Jul 18, 2014 at 7:27 am
    You need to use os/signal to intercept SIGKILL or SIGTERM and exit cleanly
    so your children continue to live.

    On Thu, Jul 17, 2014 at 8:12 PM, Hein Meling wrote:

    Your solution seems to work when then parent process exits cleanly. But if
    you do:

    func main() {
    cmd := exec.Command("sleep", "15")
    cmd.Start()
    time.Sleep(20 * time.Second)
    }

    And CTRL-C the parent process before it exits cleanly, then also the sleep
    process goes away. Does the same happen to your monkey.sh process?

    Moreover, my use case is actually somewhat more involved. That is, I would
    like to do cmd.Wait() (from different goroutines), in order to monitor the
    child processes, but I don't want the child processes to fail if the parent
    fails.

    :) Hein

    On Thursday, July 17, 2014 7:04:25 PM UTC-7, Shawn Milochik wrote:
    On Thu, Jul 17, 2014 at 9:56 PM, Hein Meling wrote:

    No, because if the go program (the parent) is killed, the children also
    gets killed.

    Hein :)

    That doesn't seem to be the case.

    package main

    import (
    "os/exec"
    )

    func main() {
    cmd := exec.Command("/tmp/monkey.sh")
    cmd.Start()
    }

    When I run that, with monkey.sh being a program that just appends text
    to a file, it keeps running after the Go program exits. I can even run the
    Go program multiple times and multiple instances of monkey.sh are running.



    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Lance Taylor at Jul 18, 2014 at 8:07 am

    On Thu, Jul 17, 2014 at 8:12 PM, Hein Meling wrote:
    Your solution seems to work when then parent process exits cleanly. But if
    you do:

    func main() {
    cmd := exec.Command("sleep", "15")
    cmd.Start()
    time.Sleep(20 * time.Second)
    }

    And CTRL-C the parent process before it exits cleanly, then also the sleep
    process goes away. Does the same happen to your monkey.sh process?

    Moreover, my use case is actually somewhat more involved. That is, I would
    like to do cmd.Wait() (from different goroutines), in order to monitor the
    child processes, but I don't want the child processes to fail if the parent
    fails.
    ^C kills all the processes because on Unix typing ^C on the terminal
    sends a signal to every process in the process group. So you want to
    have a child process that is in a different process group. You do
    that by setting Setpgid to true in the Cmd.SysProcAttr field.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Hein Meling at Jul 18, 2014 at 3:06 pm
    Thanks Ian, that worked as expected!!

    :) Hein
    On Friday, July 18, 2014 1:07:44 AM UTC-7, Ian Lance Taylor wrote:


    ^C kills all the processes because on Unix typing ^C on the terminal
    sends a signal to every process in the process group. So you want to
    have a child process that is in a different process group. You do
    that by setting Setpgid to true in the Cmd.SysProcAttr field.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Calinseciu at Oct 20, 2015 at 9:43 pm
    Setting Setpgid to true in Cmd.SysProcAttr works out great on platforms
    like OS X and Ubuntu (the only ones I tested on), but for some reason it
    doesn't appear to work on CentOS 7.1.1503 kernel version 3.10.0-229.4.2.el7.x86_64.
    Anyone else having this problem?
    On Friday, July 18, 2014 at 11:07:44 AM UTC+3, Ian Lance Taylor wrote:

    On Thu, Jul 17, 2014 at 8:12 PM, Hein Meling <hein....@gmail.com
    <javascript:>> wrote:
    Your solution seems to work when then parent process exits cleanly. But if
    you do:

    func main() {
    cmd := exec.Command("sleep", "15")
    cmd.Start()
    time.Sleep(20 * time.Second)
    }

    And CTRL-C the parent process before it exits cleanly, then also the sleep
    process goes away. Does the same happen to your monkey.sh process?

    Moreover, my use case is actually somewhat more involved. That is, I would
    like to do cmd.Wait() (from different goroutines), in order to monitor the
    child processes, but I don't want the child processes to fail if the parent
    fails.
    ^C kills all the processes because on Unix typing ^C on the terminal
    sends a signal to every process in the process group. So you want to
    have a child process that is in a different process group. You do
    that by setting Setpgid to true in the Cmd.SysProcAttr field.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 18, '14 at 1:13a
activeOct 20, '15 at 9:43p
posts9
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase