Creating a service

Some applications are capable of automatically handling their service file, and this is what we will try to achieve, step by step. Let's start with an init.d script:

#!/bin/sh"/path/to/mydaemon" $1

This is a sample script that passes the first argument to the daemon. The path to the binary will be dependent on where the file is located. This needs to be defined at runtime:

// ErrSudo is an error that suggest to execute the command as super user// It will be used with the functions that fail because of permissionsvar ErrSudo errorvar (    bin string    cmd string)func init() {    p, err := filepath.Abs(filepath.Dir(os.Args[0]))    if err != nil {        panic(err)    }    bin = p    if len(os.Args) != 1 {        cmd = os.Args[1]    } ErrSudo = fmt.Errorf("try ...

Get Hands-On System Programming with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.