systemd Module¶
Create and manage systemd services.
Import¶
Functions¶
simple_service¶
Create a systemd service unit.
systemd.simple_service(
name="myapp",
exec_start="/usr/bin/myapp",
description="My Application",
user="myapp",
group="myapp",
working_dir="/opt/myapp",
env_vars={"DEBUG": "false"},
restart="always"
)
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
name |
str | required | Service name |
exec_start |
str | required | Command to execute |
description |
str | None | Service description |
user |
str | None | User to run as |
group |
str | None | Group to run as |
working_dir |
str | None | Working directory |
env_file |
str | None | Path to environment file |
env_vars |
dict | None | Environment variables |
restart |
str | "always" | Restart policy |
restart_sec |
int | 5 | Seconds before restart |
after |
list | None | Units to start after |
wants |
list | None | Soft dependencies |
requires |
list | None | Hard dependencies |
exec_stop |
str | None | Stop command |
exec_reload |
str | None | Reload command |
type |
str | "simple" | Service type |
sudo |
bool | True | Use sudo |
Returns: ChangeList with service changes.
user_simple_service¶
Create a user-level systemd service.
Creates the service in ~/.config/systemd/user/ and manages it with systemctl --user.
Parameters: Same as simple_service, plus:
| Name | Type | Description |
|---|---|---|
user |
str | User whose systemd instance to use |
docker_compose_service¶
Create a systemd service for a Docker Compose project.
systemd.docker_compose_service(
name="mystack",
compose_file="/opt/mystack/docker-compose.yml",
description="My Docker Stack"
)
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
name |
str | required | Service name |
compose_file |
str | required | Path to docker-compose.yml |
description |
str | None | Service description |
project_name |
str | None | Compose project name |
env_file |
str | None | Environment file |
sudo |
bool | True | Use sudo |
Change Types¶
The module defines these change types:
SystemdServiceAdd— New service createdSystemdServiceModify— Service file modifiedSystemdServiceReload— Service reloaded/restarted
Examples¶
Gunicorn Service¶
from fscm.modules import systemd
systemd.simple_service(
name="webapp",
exec_start="/opt/webapp/venv/bin/gunicorn -w 4 -b unix:/run/webapp.sock app:app",
description="Web Application",
user="www-data",
group="www-data",
working_dir="/opt/webapp",
env_file="/etc/webapp/env",
restart="always",
after=["network.target", "postgresql.service"]
)
Celery Worker¶
from fscm.modules import systemd
systemd.simple_service(
name="celery-worker",
exec_start="/opt/app/venv/bin/celery -A tasks worker --loglevel=info",
description="Celery Worker",
user="celery",
working_dir="/opt/app",
env_file="/etc/app/env",
restart="always",
after=["network.target", "redis.service"]
)