Skip to content

systemd

The systemd module controls systemd service units. It can manage existing services or create new ones from scratch when a command parameter is provided.

systemd "nginx" {
state "started"
enabled #true
}
systemd "old-service" {
state "stopped"
enabled #false
}

When command is provided, the module generates a systemd unit file, uploads it to /etc/systemd/system/, and runs daemon-reload before managing the service state.

systemd "my-app" {
command "/usr/bin/my-app --port 8080"
description "My Application"
user "www-data"
group "www-data"
working-dir "/opt/my-app"
restart-policy "always"
type "simple"
after "network.target"
wanted-by "multi-user.target"
environment {
PORT "8080"
NODE_ENV "production"
}
state "started"
enabled #true
}

Minimal form — only command is required for service creation:

systemd "my-script" {
command "/usr/local/bin/my-script.sh"
state "started"
}
ParameterTypeDescription
(positional)stringUnit name
statestring"started", "stopped", or "restarted"
enabledbooleantrue or false — controls boot-time start

These parameters are only used when command is present.

ParameterTypeDefaultMaps to
commandstring(required)ExecStart=
descriptionstring"{name} service"Description=
userstring(omitted)User=
groupstring(omitted)Group=
working-dirstring(omitted)WorkingDirectory=
restart-policystring"on-failure"Restart=
typestring"simple"Type=
afterstring"network.target"After=
wanted-bystring"multi-user.target"WantedBy=
environmentmap(omitted)Environment="K=V" lines

For existing services, the module checks systemctl is-active and systemctl is-enabled before acting. If the service is already in the desired state, no action is taken.

For service creation, the module computes a SHA256 hash of the generated unit file and compares it with the remote file. The unit file is only uploaded when the content differs. After uploading, systemctl daemon-reload runs automatically.

The restarted state always triggers a restart regardless of current state.

step "Deploy app service" {
systemd "webapp" {
command "/opt/webapp/bin/server"
description "Web Application Server"
user "webapp"
working-dir "/opt/webapp"
restart-policy "always"
environment {
DATABASE_URL "postgres://localhost/mydb"
PORT "3000"
}
state "started"
enabled #true
}
}
step "Configure services" {
systemd "nginx" {
state "started"
enabled #true
}
systemd "postgresql" {
state "started"
enabled #true
}
}