Detached Daemon Process

We will create a detached child process in Go that continues to run even after the parent process has exited.

Child Process

Log CPU usage into file cpu_usage.log


package main

/* Import/include external packages/modules into this program */
import (
    "log"
    "os"
    "time"
    "github.com/shirou/gopsutil/v3/cpu"
)

func main() {
    /* Opens or creates cpu_usage.log with appropriate permissions. */
    logFile, err := os.OpenFile("cpu_usage.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
    if err != nil {
        log.Fatalf("failed to open log file: %v", err)
    }

    /* Ensures the file is closed properly using defer
     defer keyword is used to delay the execution of a 
     function until the surrounding function completes
    */
    defer logFile.Close()

    /* log module is a standard library package that provides 
    basic logging functionalities. It allows you to print log messages 
    to the console or write them to a file. Initializes a logger that 
    writes log entries to the log file with date, time, and short file information
     */
    logger := log.New(logFile, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)

    for {   /* Infinite loop */

        /* Query the CPU usage percentage over a 1 sec interval. */
        usage, err := cpu.Percent(time.Second, false)
        if err != nil {
            logger.Printf("failed to get CPU usage: %v", err)
            continue
        }

        /* Logs the CPU usage percentage to the log file */
        logger.Printf("CPU Usage: %.2f%%", usage[0])
        
        /* Sleep for 10 seconds */
        time.Sleep(10 * time.Second)
    }
}
                    
Build code

$ go build test.go
no required module provides package github.com/shirou/gopsutil/v3/cpu