Simple process manager helper library, features:
- start processes
.AddCommand(Cmd)
and.Start(cmdId)
or.StartAll()
, with environment variablesCmd.Env=[]string{}
andCmd.InheritEnv=true
- stop them;
.Kill(cmdId)
or.Cleanup()
to kill all process - restart them when they crash; using
Cmd.RestartDelayMs=1000
(=1s, default is 0) andCmd.MaxRestart=5
(restart 5x if process ended/crashed) - relay termination signals;
.Signal(cmdId, os.Kill)
- read their stdout and stderr;
Cmd.OnStdout
,Cmd.OnStdErr
callback - ability to stop processes when main processes are SIGKILL'ed:
.Cleanup()
called automatically when main process killed; - configurable backoff strategy for restarts; you can use
Cmd.OnRestart
callback to return random delay or implement your own exponential backoff, setting this callback will renderCmd.RestartDelayMs
unusable Cmd.OnExit
callback when no more restart reached, you can call.Start(cmdId)
manually again after thisCmd.OnProcessCompleted
callback each time program completed once (before restarting if MaxRestart not yet reached)Cmd.StartDelayMs=1000
(=1s, default is 0) for delaying start, in millisecondsCmd.UseChannelApi=true
, if enabled, you can receive from channels:Cmd.StderrChannel
,Cmd.OnStdoutChannel
,Cmd.ProcessCompletedChannel
,Cmd.ExitChannel
Cmd.LastExecutionError
property to get last process execution error, check this answer to get the exit codeCmd.OnStateChanged
callback andCmd.StateChangedChannel
channel to track process state- should work on Linux, and probably MacOS and Windows (untested).
- see example/ for other usage example/demo;
daemon := goproc.New()
cmdId := daemon.AddCommand(&goproc.Cmd{
Program: `sleep`, // program to run
Parameters: []string{`2`}, // command line arguments
MaxRestart: goproc.RestartForever, // default: NoRestart=0
OnStderr: func(cmd *goproc.Cmd, line string) error { // optional
fmt.Println(`OnStderr: `+line)
return nil
},
OnStdout: func(cmd *goproc.Cmd, line string) error { // optional
fmt.Println(`OnStdout: `+line)
return nil
},
})
daemon.Start(cmdId) // use "go" keyword if you need non-blocking version
- implement
.Pause
and.Resume
API - comments and documentation in code;
- continuous integration configuration;
- integration tests;
- unit tests.