go 守护进程

梦想游戏人
目录:
Go

方便进行自动拉起

/*
* Email:   me@dreamyouxi.com

go ChatServer Deamon process

*/
package main

import "fmt"
import "time"
import "os"

//main func for start up server
func main() {
	for {
		//record std error to file
		ff, er := os.OpenFile("go_stderr.txt",os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0766)
		if er!=nil{
			continue;
		}
		proc, err := os.StartProcess("ChatServer.exe", nil, &os.ProcAttr{Files: []*os.File{os.Stdin, os.Stdout,ff}})
		if err!=nil{
		}
		_,err = proc.Wait();
		ff.WriteString(time.Now().Format("--------[2006-01-02 15:04:05]") )
		ff.WriteString("\r\n\r\n");
		
		ff.Close();
		// 进程启动需要等待3s 如果是crash时 需要等待系统回收进程资源
		fmt.Println("-------------ChatServer crash will restart after 3 second");
		time.Sleep(time.Duration(3)*time.Second)
	}
}
Scroll Up