Example #1
0
func (ss *SubService) rerunner(rerunChan chan bool) {
	var proc *os.Process
	for rerun := range rerunChan {
		if !rerun {
			break
		}

		cmd := exec.Command(ss.binPath, ss.argv...)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		startupTimer := time.NewTimer(RerunWait)

		cmd.Start()
		proc = cmd.Process

		// In another goroutine, wait for the process to complete and send a relaunch signal.
		// If this signal is sent after the stop signal, it is ignored.
		go func(proc *os.Process) {
			proc.Wait()
			select {
			case <-startupTimer.C:
				// we let it run long enough that it might not be a recurring error, try again
				rerunChan <- true
			default:
				// error happened too quickly - must be startup issue
				startupTimer.Stop()
			}
		}(proc)
	}
	proc.Kill()
}
Example #2
0
func stopServer(proc *os.Process) {
	err := proc.Kill()
	if err != nil {
		panic(err.Error())
	}
	proc.Release()
}
Example #3
0
func run(binName, binPath string, args []string) (runch chan bool) {
	runch = make(chan bool)
	go func() {
		cmdline := append([]string{binName}, args...)
		var proc *os.Process
		for relaunch := range runch {
			if proc != nil {
				err := proc.Signal(os.Interrupt)
				if err != nil {
					log.Printf("error on sending signal to process: '%s', will now hard-kill the process\n", err)
					proc.Kill()
				}
				proc.Wait()
			}
			if !relaunch {
				continue
			}
			cmd := exec.Command(binPath, args...)
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr
			log.Print(cmdline)
			err := cmd.Start()
			if err != nil {
				log.Printf("error on starting process: '%s'\n", err)
			}
			proc = cmd.Process
		}
	}()
	return
}
Example #4
0
func run(ch chan bool, bin string, args []string) {
	go func() {
		var proc *os.Process

		for relaunch := range ch {
			if proc != nil {
				if err := proc.Signal(os.Interrupt); err != nil {
					proc.Kill()
				}
				proc.Wait()
			}

			if !relaunch {
				continue
			}

			cmd := exec.Command(bin, args...)
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr

			if err := cmd.Start(); err != nil {
				log("error: %s", err)
			}

			proc = cmd.Process
		}
	}()
	return
}
Example #5
0
// runBin runs the generated bin file with the arguments expected
func runBin(bindir, bin string, args []string) chan bool {
	var relunch = make(chan bool)
	go func() {
		binfile := fmt.Sprintf("%s/%s", bindir, bin)
		// cmdline := append([]string{bin}, args...)
		var proc *os.Process

		for dosig := range relunch {
			if proc != nil {
				if err := proc.Signal(os.Interrupt); err != nil {
					log.Printf("Error in sending signal %s", err)
					proc.Kill()
				}
				proc.Wait()
			}

			if !dosig {
				continue
			}

			cmd := exec.Command(binfile, args...)
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr

			if err := cmd.Start(); err != nil {
				log.Printf("Error starting process: %s", err)
			}

			proc = cmd.Process
		}
	}()
	return relunch
}
Example #6
0
func (h *Hoverfly) stop(hoverflyDirectory HoverflyDirectory) error {
	if !h.isLocal() {
		return errors.New("hoverctl can not stop an instance of Hoverfly on a remote host")
	}

	pid, err := hoverflyDirectory.GetPid(h.AdminPort, h.ProxyPort)

	if err != nil {
		log.Debug(err.Error())
		return errors.New("Could not read Hoverfly pid file")
	}

	if pid == 0 {
		return errors.New("Hoverfly is not running")
	}

	hoverflyProcess := os.Process{Pid: pid}
	err = hoverflyProcess.Kill()
	if err != nil {
		log.Info(err.Error())
		return errors.New("Could not kill Hoverfly")
	}

	err = hoverflyDirectory.DeletePid(h.AdminPort, h.ProxyPort)
	if err != nil {
		log.Debug(err.Error())
		return errors.New("Could not delete Hoverfly pid")
	}

	return nil
}
Example #7
0
// Run the command in the given string and restart it after
// a message was received on the buildDone channel.
func runner(command string, buildDone <-chan struct{}) {
	var currentProcess *os.Process
	pipeChan := make(chan io.ReadCloser)

	go logger(pipeChan)

	for {
		<-buildDone

		if currentProcess != nil {
			if err := currentProcess.Kill(); err != nil {
				log.Fatal("Could not kill child process. Aborting due to danger of infinite forks.")
			}

			_, werr := currentProcess.Wait()

			if werr != nil {
				log.Fatal("Could not wait for child process. Aborting due to danger of infinite forks.")
			}
		}

		log.Println("Restarting the given command.")
		cmd, stdoutPipe, stderrPipe, err := startCommand(command)

		if err != nil {
			log.Fatal("Could not start command:", err)
		}

		pipeChan <- stdoutPipe
		pipeChan <- stderrPipe

		currentProcess = cmd.Process
	}
}
Example #8
0
func simpleKill(p *os.Process) error {
	if p == nil {
		return nil
	}
	Log.Info("killing", "pid", p.Pid)
	defer recover()
	return p.Kill()
}
Example #9
0
// Go doesn't support signals in windows (pre Go 1.3) because windows doesn't have signals.
// Windows implementation will use kill
func stop(process *os.Process) error {
	err := process.Kill()
	if err != nil {
		log.Println("Error while sending SIG_DEATH to running server:", err)
		return err
	}
	return nil
}
Example #10
0
// Log a call to os.Process.Kill.
func logKill(p *os.Process) error {
	log.Printf("killing PID %d", p.Pid)
	err := p.Kill()
	if err != nil {
		log.Print(err)
	}
	return err
}
Example #11
0
// RunGo runs the generated binary file with the arguments expected
func RunGo(gofile string, args []string, done, stopped func()) chan bool {
	var relunch = make(chan bool)

	// if runtime.GOOS == "windows" {
	gofile = filepath.Clean(gofile)
	// }

	go func() {

		// var cmdline = fmt.Sprintf("go run %s", gofile)
		cmdargs := append([]string{"run", gofile}, args...)
		// cmdline = strings.Joinappend([]string{}, "go run", gofile)

		var proc *os.Process

		for dosig := range relunch {
			if proc != nil {
				var err error

				if runtime.GOOS == "windows" {
					err = proc.Kill()
				} else {
					err = proc.Signal(os.Interrupt)
				}

				if err != nil {
					fmt.Printf("---> Error in Sending Kill Signal %s\n", err)
					proc.Kill()
				}
				proc.Wait()
				proc = nil
			}

			if !dosig {
				continue
			}

			fmt.Printf("--> Starting cmd: %s\n", cmdargs)
			cmd := exec.Command("go", cmdargs...)
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr

			if err := cmd.Start(); err != nil {
				fmt.Printf("---> Error starting process: %s\n", err)
			}

			proc = cmd.Process
			if done != nil {
				done()
			}
		}

		if stopped != nil {
			stopped()
		}
	}()
	return relunch
}
Example #12
0
func kill(p *os.Process) {
	verboseLog("Killing child process")
	if err := p.Kill(); err != nil {
		traceLog("Could not kill", ApplicationName, "child is still running with PID:", p.Pid)
	}

	verboseLog("Removing test artifacts")
	os.Remove(ApplicationPIDFile)
	os.Remove(ApplicationName)
}
Example #13
0
func killProcessHard(process *os.Process) {
	log.Println(okColor("Hard stopping the current process.."))

	if err := process.Kill(); err != nil {
		log.Fatal(failColor("Could not kill child process. Aborting due to danger of infinite forks."))
	}

	if _, err := process.Wait(); err != nil {
		log.Fatal(failColor("Could not wait for child process. Aborting due to danger of infinite forks."))
	}
}
Example #14
0
func killCamli(proc *os.Process) {
	if *fast {
		return
	}
	dbg.Println("killing dev server")
	err := proc.Kill()
	if err != nil {
		log.Fatalf("Could not kill dev-server: %v", err)
	}
	dbg.Println("")
}
Example #15
0
File: rproc.go Project: hoffoo/envR
// listen for signal and forward to process
func listenSignal(proc *os.Process) {

	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGTERM, syscall.SIGQUIT)

	go func() {
		for {
			<-sig
			proc.Kill()
		}
	}()
}
Example #16
0
func groupKill(p *os.Process, interrupt bool) error {
	if p == nil {
		return nil
	}
	Log.Info("groupKill", "pid", p.Pid)
	defer recover()
	if interrupt {
		defer p.Signal(os.Interrupt)
		return GroupKill(p.Pid, os.Interrupt)
	}
	defer p.Kill()
	return GroupKill(p.Pid, os.Kill)
}
Example #17
0
// RunBin runs the generated binary file with the arguments expected
func RunBin(binfile string, args []string, done, stopped func()) chan bool {
	var relunch = make(chan bool)
	go func() {
		// binfile := fmt.Sprintf("%s/%s", bindir, bin)
		// cmdline := append([]string{bin}, args...)
		var proc *os.Process

		for dosig := range relunch {
			if proc != nil {
				var err error

				if runtime.GOOS == "windows" {
					err = proc.Kill()
				} else {
					err = proc.Signal(os.Interrupt)
				}

				if err != nil {
					fmt.Printf("---> Error in Sending Kill Signal: %s\n", err)
					proc.Kill()
				}
				proc.Wait()
				proc = nil
			}

			if !dosig {
				continue
			}

			fmt.Printf("--> Starting bin: %s\n", binfile)
			cmd := exec.Command(binfile, args...)
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr

			if err := cmd.Start(); err != nil {
				fmt.Printf("---> Error starting process: %s -> %s\n", binfile, err)
			}

			proc = cmd.Process
			if done != nil {
				done()
			}
		}

		if stopped != nil {
			stopped()
		}
	}()
	return relunch
}
Example #18
0
func prepareBuild(args cliArgs, config caddybuild.Config) (App, error) {
	caddybuild.SetConfig(config)

	var builder custombuild.Builder
	var f *os.File
	var caddyProcess *os.Process
	// remove temp files.
	addCleanUpFunc(func() {
		if caddyProcess != nil {
			caddyProcess.Kill()
		}
		builder.Teardown()
		if f != nil {
			os.Remove(f.Name())
		}
	})

	builder, err := caddybuild.PrepareBuild(config.Plugins, false)
	exitIfErr(err)

	// if output is set, generate binary only.
	if args.output != "" {
		err := saveCaddy(builder, args.output, args.goArgs)
		exitIfErr(err)
		os.Exit(0)
	}

	// create temporary file for binary
	f, err = ioutil.TempFile("", "caddydev")
	exitIfErr(err)
	f.Close()

	// fix for windows exe
	fName := f.Name()
	if runtime.GOOS == "windows" {
		fName += ".exe"
	}

	return &caddyApp{
		file:      fName,
		goArgs:    args.goArgs,
		caddyArgs: args.caddyArgs,
		builder:   builder,
	}, nil
}
Example #19
0
func run(binName, binPath string, args []string) (runch chan bool) {
	runch = make(chan bool)
	go func() {
		cmdline := append([]string{binName}, args...)
		var proc *os.Process
		for _ = range runch {
			if proc != nil {
				proc.Kill()
			}
			cmd := exec.Command(binPath, args...)
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr
			log.Print(cmdline)
			cmd.Start()
			proc = cmd.Process
		}
	}()
	return
}
Example #20
0
func terminateProcess(process *os.Process, gracefulTimeoutSecs int) bool {
	pid := process.Pid

	process.Signal(os.Interrupt)

	stillRunning := true
	for i := 0; i < gracefulTimeoutSecs; i++ {
		if p, _ := os.FindProcess(pid); p == nil {
			stillRunning = false
			break
		}
		time.Sleep(1 * time.Second)
	}
	// Done our best... hard kill
	if stillRunning {
		process.Kill()
		return false
	}
	return true
}
Example #21
0
func handleKillCamliSignal(camliProc *os.Process) {
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
	for {
		sig := <-c
		sysSig, ok := sig.(syscall.Signal)
		if !ok {
			log.Fatal("Not a unix signal")
		}
		switch sysSig {
		case syscall.SIGTERM, syscall.SIGINT:
			log.Print("Received kill signal, terminating.")
			err := camliProc.Kill()
			if err != nil {
				log.Fatalf("Failed to kill camli: %v ", err)
			}
		default:
			log.Fatal("Received another signal, should not happen.")
		}
	}
}
Example #22
0
func (ss *SubService) rerunner(rerunChan chan bool) {
	var proc *os.Process
	for rerun := range rerunChan {
		if !rerun {
			break
		}

		cmd := exec.Command(ss.binPath, ss.argv...)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		cmd.Start()
		proc = cmd.Process

		// In another goroutine, wait for the process to complete and send a relaunch signal.
		// If this signal is sent after the stop signal, it is ignored.
		go func(proc *os.Process) {
			proc.Wait()
			rerunChan <- true
		}(proc)
	}
	proc.Kill()
}
Example #23
0
// KillWithChildren kills the process
// and tries to kill its all children (process group)
func KillWithChildren(p *os.Process, interrupt bool) (err error) {
	Log.Debug("killWithChildren", "process", p)
	if p == nil {
		return
	}
	Log.Info("killWithChildren", "pid", p.Pid, "interrupt", interrupt)
	defer func() {
		if r := recover(); r != nil {
			Log.Warn("PANIC in kill", "process", p, "error", r)
		}
	}()
	defer p.Release()
	if p.Pid == 0 {
		return nil
	}
	if interrupt {
		defer p.Signal(os.Interrupt)
		return Pkill(p.Pid, os.Interrupt)
	}
	defer p.Kill()
	return Pkill(p.Pid, os.Kill)
}
Example #24
0
func terminateProcess(process *os.Process, gracefulTimeoutSecs int) bool {
	pid := process.Pid

	// In Windows we'll send a Ctrl Break and also use taskkill.exe just in case it's a
	// wmain Windows program.
	sendCtrlBreak(pid)
	sendTaskKill(pid)

	stillRunning := true
	for i := 0; i < gracefulTimeoutSecs; i++ {
		if p, _ := os.FindProcess(pid); p == nil {
			stillRunning = false
			break
		}
		time.Sleep(1 * time.Second)
	}
	// Done our best... hard kill
	if stillRunning {
		process.Kill()
		return false
	}
	return true
}
Example #25
0
func cleanup(key string, log plugin.Logger) error {
	pids, err := listProc()
	if err != nil {
		return err
	}
	pidMarker := fmt.Sprintf("EVR_AGENT_PID=%v", os.Getpid())
	taskMarker := fmt.Sprintf("EVR_TASK_ID=%v", key)
	for _, pid := range pids {
		env, err := getEnv(pid)
		if err != nil {
			continue
		}
		if envHasMarkers(env, pidMarker, taskMarker) {
			p := os.Process{}
			p.Pid = pid
			if err := p.Kill(); err != nil {
				log.LogSystem(slogger.INFO, "Cleanup killing %v failed: %v", pid, err)
			} else {
				log.LogTask(slogger.INFO, "Cleanup killed process %v", pid)
			}
		}
	}
	return nil
}
Example #26
0
func killProcessTreeUnix(p *os.Process) error {
	return p.Kill()
}
Example #27
0
func testStopConsulServer(t *testing.T, p *os.Process) {
	p.Kill()
}
Example #28
0
func killNetNsProc(proc *os.Process) {
	proc.Kill()
	proc.Wait()
}
Example #29
0
func KillHydraProcess(process *os.Process) {
	process.Kill()
}
Example #30
0
func main() {
	// parse cli arguments.
	args, err := parseArgs()
	exitIfErr(err)

	// read config file.
	config, err := readConfig(args)
	exitIfErr(err)

	fetched := false
	// if caddy source does not exits, pull source.
	if !isLocalPkg(caddybuild.CaddyPackage) {
		fmt.Print("Caddy source not found. Fetching...")
		err := fetchCaddy()
		exitIfErr(err)
		fmt.Println(" done.")
		fetched = true
	}

	// if update flag is set, pull source.
	if args.update && !fetched {
		fmt.Print("Updating caddy source...")
		err := fetchCaddy()
		exitIfErr(err)
		fmt.Println(" done.")
	}

	caddybuild.SetConfig(config)

	var builder custombuild.Builder
	var f *os.File
	var caddyProcess *os.Process
	// remove temp files.
	addCleanUpFunc(func() {
		if caddyProcess != nil {
			caddyProcess.Kill()
		}
		builder.Teardown()
		if f != nil {
			os.Remove(f.Name())
		}
	})

	builder, err = caddybuild.PrepareBuild(features.Middlewares{config.Middleware})
	exitIfErr(err)

	// if output is set, generate binary only.
	if args.output != "" {
		err := saveCaddy(builder, args.output, args.goArgs)
		exitIfErr(err)
		return
	}

	// create temporary file for binary
	f, err = ioutil.TempFile("", "caddydev")
	exitIfErr(err)
	f.Close()

	// perform custom build
	err = builder.Build("", "", f.Name(), args.goArgs...)
	exitIfErr(err)

	fmt.Println("Starting caddy...")

	// trap os interrupts to ensure cleaning up temp files.
	done := trapInterrupts()

	// start custom caddy.
	go func() {
		cmd, err := startCaddy(f.Name(), args.caddyArgs)
		exitIfErr(err)
		caddyProcess = cmd.Process
		err = cmd.Wait()
		cleanUp()
		exitIfErr(err)
		done <- struct{}{}
	}()

	// wait for exit signal
	<-done

}