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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.