GuidesModules
service.running
Ensures a service is started on the target system. When triggered via a watch requisite (e.g., a config file changed), it restarts the service rather than starting it.
Source: pkg/state/modules/service.go
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | No | State ID | The service name. If omitted, the state ID is used. |
enable | bool | No | false | Also enable the service at boot when applying. |
All states also accept the full set of requisite parameters and Salt-parity state attributes — see Dependencies & Requisites. Note that a watch requisite force-applies this state, triggering a service restart.
Check Behavior
- Calls
systemctl is-active --quiet <service>(via the injected ServiceExec). - Reports no changes needed if the service is already running.
- Reports
NeedsChange: trueif the service is not running.
Apply Behavior
- Checks whether the service is currently running.
- If the service is not running: calls
Startand records that it started (for Revert). - If the service is already running (e.g., triggered by watch): calls
Restart. - If
enable: trueand the service is not yet enabled: callsEnableand records that it enabled (for Revert). - Returns
Changed: truein all cases where action was taken.
Revert Behavior
- If Apply enabled the service: calls
Disable. - If Apply started the service: calls
Stop. - If Apply only restarted the service (was already running): returns
Changed: false(no revert possible).
Details Returned
| Key | Description |
|---|---|
service | The service name |
action | "started" or "restarted" |
manager | The service manager name (e.g., "systemd") |
Examples
Ensure nginx is running
nginx:
service.running:
- require:
- pkg.installed:nginxRestart nginx when config changes
nginx:
service.running:
- watch:
- file.managed:/etc/nginx/nginx.conf
- file.managed:/etc/nginx/sites-enabled/defaultEnsure running and enabled at boot
nginx:
service.running:
- enable: true
- require:
- pkg.installed:nginxFull example with explicit name
webserver-running:
service.running:
- name: nginx
- enable: true
- require:
- pkg.installed:nginx
- watch:
- file.managed:/etc/nginx/nginx.conf