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 permissions
var ErrSudo error
var (
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 `sudo %s %s`", bin, cmd)
}
The main function will handle the...