child_process_manager

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 3 Imported by: 1

README

Child process manager for go

This is a simple library to ensure all the child processes are killed if the main process is killed.

Usage

Import the module

go get github.com/AgustinSRG/go-child-process-manager

Example usage:

package main

import (
	"os/exec"

	// Import the module
	child_process_manager "github.com/AgustinSRG/go-child-process-manager"
)

func main() {
	// The child process manager must be initialized before any child processes are created
	err := child_process_manager.InitializeChildProcessManager()
	if err != nil {
		panic(err)
	}
	// Call DisposeChildProcessManager() just before exiting the main process
	defer child_process_manager.DisposeChildProcessManager()

	// Create a command (this is an example)
	cmd := exec.Command("ffmpeg", "-i", "input.mp4", "output.webm")

	// Configure the command to be killed when the main process dies
	err = child_process_manager.ConfigureCommand(cmd)
	if err != nil {
		panic(err)
	}

	// Start the process
	err = cmd.Start()
	if err != nil {
		panic(err)
	}

	// Add process as a child process
	err = child_process_manager.AddChildProcess(cmd.Process)
	if err != nil {
		cmd.Process.Kill() // We must kill the process if this fails
		panic(err)
	}

	// Wait for the process to finish
	err = cmd.Wait()
	if err != nil {
		panic(err)
	}
}

Testing

In order to test the code, first go to the test folder.

cd test

Then, build the test binary:

go get github.com/AgustinSRG/go-child-process-manager/test
go build

Run the test binary:

./test

After it finishes, it should print SUCCESS | THE CHILD PROCESS IS DEAD

If you want to test what happens without using this library, use the following:

./test --no-library

After it finishes, it should print ERROR | THE CHILD PROCESS WAS ALIVE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddChildProcess

func AddChildProcess(p *os.Process) error

Adds a child process, so it is killed if the main process dies p - Child process

func ConfigureCommand

func ConfigureCommand(cmd *exec.Cmd) error

Configures the command to be a child process, use this before you call Start() cmd - Command reference

func DisposeChildProcessManager

func DisposeChildProcessManager() error

Disposes resources created for the process manager Call this before the main process ends

func InitializeChildProcessManager added in v1.0.1

func InitializeChildProcessManager() error

Initializes child process manager

Types

This section is empty.

Jump to

Keyboard shortcuts

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