O'Reilly logo

Hands-On System Programming with Go by Alex Guerrieri

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required