unitard/README.md

61 lines
2.2 KiB
Markdown
Raw Permalink Normal View History

2022-11-24 19:09:42 +10:30
# unitard - automatically deploy a systemd unit file from your application
2022-11-24 20:00:06 +10:30
[![Go Reference](https://pkg.go.dev/badge/github.com/tardisx/unitard.svg)](https://pkg.go.dev/github.com/tardisx/unitard)
2022-11-24 19:09:42 +10:30
## Synopsis
import "github.com/tardisx/unitard"
func main() {
2022-11-24 20:08:25 +10:30
appName := "coolapp"
if deploy {
// error checking ignored for this example
unit, _ := unitard.NewUnit(appName)
unit.Deploy()
os.Exit(0)
}
// rest of your application here
2022-11-24 19:09:42 +10:30
}
## What it does
2022-11-24 20:08:25 +10:30
This package provides a simple interface to automatically creating and enabling a
systemd "user unit" service, as part of your single-binary deployment.
The `Deploy()` function creates the systemd unit file, reloads the systemd daemon
so it reads it, enables the unit (to start on boot) and starts the service
2022-11-24 19:09:42 +10:30
running.
2022-11-24 20:08:25 +10:30
Copy your executable to "somewhere" on your target system, run it with `-deploy`
(or however you have enabled the call to `Deploy()`) and your application starts
running in the background and will restart on boot.
2022-11-24 19:09:42 +10:30
2022-11-24 20:08:25 +10:30
There is also an `Undeploy()` func, which you should of course provide as an option
to your users. It stops the running service, removes the unit file and reloads the
systemd daemon.
2022-11-24 19:09:42 +10:30
2022-11-24 23:01:50 +10:30
While it does shell out to call the `systemctl` tool, this does mean this package
adds no new non-core dependencies to your project.
2022-11-24 19:09:42 +10:30
## What's with the name?
It's the systemd UNIT for Automatic Restart Deployment. Or, just a stupid pun based on my username.
## Does this work for root?
It's designed to not. It leverages the systemd `--user` facility, where users can configure
their own services to run persistently, with all configuration being done out of their home
directory (in `~/.config/systemd`).
See https://wiki.archlinux.org/title/Systemd/User for more information.
## It works! Until I logout, and then my program stops!
You need to enable "lingering" - see the link above.
## I want it to do X
It's designed to be almost zero configuration - you just provide an application name
(which gets used to name the `.service` file). This is by intent.
However it's not impossible that there are sensible user-configurable things. Raise an issue.