🔄 service
🔄 service
Manage system services — start, stop, enable, disable, restart, reload.
⚡ Actions
| Action | Description |
|---|---|
:nothing |
Do nothing (default) — use with notifications |
:start |
Start the service (only if not running) |
:stop |
Stop the service (only if running) |
:restart |
Restart the service (always runs) |
:reload |
Reload the service config (only if running) |
:enable |
Enable the service at boot (only if not enabled) |
:disable |
Disable the service at boot (only if enabled) |
⚠️ Note: The default action is
:nothing, not:start. This is intentional — services are typically used as notification targets. You must explicitly setactionif you want the service to start/enable.
📋 Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
name |
String | Resource name | Service name (auto-set from resource name) |
provider |
Symbol | — | Service provider (:systemd, :upstart, etc.) |
💡 When
provideris set, Itamae appends_under_<provider>to all specinfra method calls. For example, withprovider :systemd, it callscheck_service_is_running_under_systemdinstead ofcheck_service_is_running.
🔍 How It Works
- Query — Checks if the service is currently running and enabled
- Compare — Determines which state changes are needed
- Execute — Runs only the necessary operations:
:startand:stopare conditional (check state first):restartalways runs unconditionally:reloadonly runs if the service is currently running:enableand:disableare conditional (check state first)
🔬 Dry-Run Behavior
Running and enabled states are queried from the system. You see which services would start, stop, enable, or disable. No service state changes are applied.
📖 Examples
Enable and start a service
service 'nginx' do
action [:enable, :start]
end
Restart on configuration change
service 'nginx' do
action :nothing
end
template '/etc/nginx/nginx.conf' do
source 'nginx.conf.erb'
notifies :restart, 'service[nginx]'
end
Use a specific provider
service 'myapp' do
provider :systemd
action [:enable, :start]
end
Stop and disable
service 'apache2' do
action [:stop, :disable]
end
Reload without restart (via subscribes)
service 'nginx' do
action :nothing
subscribes :reload, 'template[/etc/nginx/conf.d/app.conf]'
end
Multiple actions in sequence
service 'app' do
action [:enable, :start]
end
💡 Actions in an array are executed in order.
[:enable, :start]first enables the service, then starts it.