Git Product home page Git Product logo

go-autostart's Introduction

go-autostart

Go library to register your app to autostart on startup (supports Linux, macOS, and Windows).

Basic example

// Define your app's autostart behavior
app := autostart.New(autostart.Options{
    Label: "com.mycompany.MyApp",
    Vendor: "Company"
    Name: "My App",
    Description: "My app description",
    Mode: autostart.ModeUser,
    Arguments: []string{ /* ... */ },
})

// Enable, disable, or check the status
app.Enable()
app.Disable()
app.IsEnabled()

// To get other useful data
app.DataDir()
app.StdOutPath()
app.StdErrPath()

Supported platforms

  • Linux (systemd)
  • macOS (launchd)
  • Windows (Service Manager)

Logging

When running your process as a service, it's a good idea to make a deliberate decision about where to send logs.

With go-autostart, you can specify file paths for both stdout and stderr. If you don't specify a path, a platform-specific default location will be chosen based on your app details.

app := autostart.New(autostart.Options{
    // ...
    StdoutPath: "/path/to/myapp.log",
    StderrPath: "/path/to/myapp.err",
})

You can create writers to your custom log files. This will override the global os.Stdout and os.Stderr with custom writers that send logs to both the program's stdio and the custom log files.

Run the following to override the global os.Stdout and os.Stderr with your custom log files. This function returns a new, wrapped version of stdout. If you still want to write logs to the console, in addition to the log file, writing to this new stdout will do both.

stdout, err := app.Stdio()

Then, to write logs to both the console and the log file:

// Write directly
fmt.Fprintln(stdout, "Hello, world!")

// Set the output for the `log` package
log.SetOutput(stdout)
log.Println("Hello, world!")

Full Example

// Define your app's autostart behavior
app := autostart.New(autostart.Options{
    Label: "com.mycompany.MyApp",
    Vendor: "Company"
    Name: "My App",
    Description: "My app description",
    Mode: autostart.ModeUser,
    Arguments: []string{ /* ... */ },
})

// Setup logging to the log files
stdout, err := app.Stdio()
log.SetOutput(stdout)

// Enable auto-start for the app
app.Enable()

go-autostart's People

Contributors

connerdouglass avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

addisonrogers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.