How to use StartProcess

3,014 views
Skip to first unread message

Hoping White

unread,
Jun 3, 2011, 6:27:43 AM6/3/11
to golang-nuts
Hi, all

I have tried to use os.StartProcess and failed always. My lately created
process exits instantly.
Can anyone give me some examples?

Hoping White

unread,
Jun 3, 2011, 6:34:18 AM6/3/11
to golang-nuts
My programs as follows:
package main

import(
"os"
)

func main() {
if len(os.Args) < 2 {
println("usage: supervisor program arg1 arg2 ...")
return
}

procAttr := new(os.ProcAttr)
var args []string
if len(os.Args) == 2 {
args = nil
} else {
args = os.Args[2:]
}

process, err := os.StartProcess(os.Args[1], args, procAttr)
if err != nil {
println("start process failed:" + err.String())
return
}

waitMsg, err := process.Wait(os.WSTOPPED)
if err != nil {
println("Wait Error:" + err.String())
}
println("waitMsg:" + waitMsg.String())
}

锟斤拷 2011/6/3 18:27, Hoping White 写锟斤拷:

unread,
Jun 3, 2011, 7:41:46 AM6/3/11
to golang-nuts
Below is a working example. Note that you have to specify the full
path to the program (e.g: /usr/bin/ls), or use exec.LookPath to lookup
the path of the program. (Tip: If you are using GOAM or a similar
tool, you can compile the source code by: create a new directory, copy
the source code from web browser into a new file in that directory,
run "gofmt -w file.go", execute "goam make").

package main

import "os"

func main() {
if len(os.Args) < 2 {
println("usage: %s program arg1 arg2 ...", os.Args[0])
return
}

var procAttr os.ProcAttr
procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}

process, err := os.StartProcess(os.Args[1], os.Args[1:],
&procAttr)
if err != nil {
println("start process failed:" + err.String())
return
}

_, err = process.Wait(0)

unread,
Jun 3, 2011, 7:50:33 AM6/3/11
to golang-nuts
Ok, some minor corrections: the 1st println is messed up, "/usr/bin/
ls" should be "/bin/ls".

chris dollin

unread,
Jun 3, 2011, 7:57:15 AM6/3/11
to Hoping White, golang-nuts
2011/6/3 Hoping White <baiha...@gmail.com>:

>> I have tried to use os.StartProcess and failed always. My lately created
>> process exits instantly.
>> Can anyone give me some examples?

Tell us what the error messages were, and what arguments
you passed, and what OS you're using.

Chris

--
Chris "allusive" Dollin

Norbert Roos

unread,
Jun 3, 2011, 8:55:15 AM6/3/11
to golan...@googlegroups.com
On 06/03/2011 12:34 PM, Hoping White wrote:

> procAttr := new(os.ProcAttr)
> var args []string
> if len(os.Args) == 2 {
> args = nil
> } else {
> args = os.Args[2:]
> }

Try

args = os.Args[1:]

instead of the complete "if" block. The program name needs to be the
first argument in the argument list.

Norbert

Hoping White

unread,
Jun 3, 2011, 9:26:17 AM6/3/11
to golan...@googlegroups.com
Hi, Roos

Thanks, it works now.
I have another question, how to make a program deamonize?
I have searched the nuts mailist, and found that the suggested thread
can't be opened.

锟斤拷 2011/6/3 20:55, Norbert Roos 写锟斤拷:

Norbert Roos

unread,
Jun 3, 2011, 10:11:52 AM6/3/11
to golan...@googlegroups.com
On 06/03/2011 03:26 PM, Hoping White wrote:

> I have another question, how to make a program deamonize?

You mean the started process should keep running, even when the program
which started the process terminates? Sorry, i don't know this... i
could imagine that this is something os specific.

Norbert

Reply all
Reply to author
Forward
0 new messages