process

package
v1.15.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2025 License: MIT Imports: 3 Imported by: 0

README

Go Process Kill Utility

A simple, cross-platform Go package for terminating processes by their PID. It prioritizes a graceful shutdown before resorting to a force kill, ensuring that applications have a chance to clean up resources.

Features

  • Cross-Platform: Works seamlessly on Windows, Linux, and macOS.
  • Graceful Shutdown First: Always attempts to terminate a process gracefully before forcing it to exit.
  • Automatic Fallback: If a graceful shutdown fails or the process does not exit within a timeout period (5 seconds), it automatically performs a force kill.
  • Simple API: A single Kill(pid) function makes it incredibly easy to use.

Usage

Here is a simple example of how to start a process and then terminate it using this package.

package main

import (
	"fmt"
	"log"
	"os/exec"
	"runtime"
	"time"

	"github.com/go-dev-frame/sponge/pkg/process"
)

func main() {
	var cmd *exec.Cmd

	// Start a long-running process appropriate for the OS.
	if runtime.GOOS == "windows" {
		cmd = exec.Command("timeout", "/t", "30")
	} else {
		cmd = exec.Command("sleep", "30")
	}

	err := cmd.Start()
	if err != nil {
		log.Fatalf("Failed to start command: %v", err)
	}

	pid := cmd.Process.Pid
	fmt.Printf("Started process with PID: %d\n", pid)

	// Give the process a moment to initialize.
	time.Sleep(1 * time.Second)
	fmt.Printf("Attempting to kill process %d...\n", pid)

	// Use the Kill function to terminate it.
	if err = process.Kill(pid); err != nil {
		log.Fatalf("Failed to kill process: %v", err)
	}

	fmt.Printf("Successfully terminated process %d.\n", pid)

	// The cmd.Wait() call will now return an error because the process was killed.
	err = cmd.Wait()
	fmt.Printf("Process wait result: %v\n", err)
}

Documentation

Overview

Package process provides functions to manage processes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Kill

func Kill(pid int) error

Kill terminates a process by its PID.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL