systemd Service Generator

Generate enterprise-ready systemd unit files with security hardening, resource limits, and auto-restart policies.

A human readable name for the unit.

A URI to the documentation (e.g. man:nginx(8)).

Start this unit after the specified units.

Start this unit before the specified units.

Strong dependency on other units.

Weak dependency on other units.

Commands with their arguments that are executed when this service is started.

Commands to execute to trigger a configuration reload.

Commands to execute to stop the service.

Run the service as this user.

Run the service as this group.

Space-separated KEY=VALUE pairs.

Absolute path to a file containing env vars.

Path to PID file, useful for Type=forking.

Time to sleep before restarting.

Maximum number of open files (e.g. 65535).

Maximum number of processes.

- Allocate a dynamic ephemeral user for this service.
- Ensure the process cannot gain new privileges (e.g. via setuid).
- Provide a private /tmp and /var/tmp.
- Sets up a new /dev mount with minimal devices.
- Makes /proc/sys, /sys, etc read-only.
- Deny module loading.
- Make /sys/fs/cgroup read-only.
- Deny creating writable and executable memory mappings.
- Restrict access to Linux namespaces.
- Deny realtime scheduling.

Target to start this unit with (e.g. multi-user.target).

myapp.service

Installation Instructions

# 1. Copy the file to systemd directory

sudo cp myapp.service /etc/systemd/system/myapp.service


# 2. Reload systemd manager configuration

sudo systemctl daemon-reload


# 3. Enable the service to start on boot

sudo systemctl enable myapp.service


# 4. Start the service immediately

sudo systemctl start myapp.service


# 5. Check the status

sudo systemctl status myapp.service

Overview

systemd is the standard init system for most modern Linux distributions (Ubuntu, Debian, RHEL, CentOS). Generating a proper systemd unit file is critical for ensuring your applications restart automatically on failure, start at boot, and are properly sandboxed from the rest of the system.

Process Management

Automatic restarts, clean shutdowns, and dependency ordering.

Sandboxing

Limit filesystem access, drop capabilities, and isolate networking.

Resource Control

Set MemoryMax, CPUQuota, and file descriptor limits (LimitNOFILE).

Best Practices for Production

  • Never run as root if the service doesn't strictly need it. Use `User=` or `DynamicUser=yes`.
  • Always set a restart policy (e.g., `Restart=on-failure`) but pair it with `StartLimitBurst` to avoid CPU spin loops.
  • Harden your unit using `ProtectSystem=strict`, `ProtectHome=yes`, and `NoNewPrivileges=yes`.
  • Use `EnvironmentFile` for secrets rather than hardcoding them in `Environment=` directives.

Troubleshooting Guide

Service fails to start (code=exited, status=203/EXEC): Check that the executable path in `ExecStart` is absolute and correct. Ensure the file has execute permissions (`chmod +x`).

Service starts but immediately exits: If using `Type=simple`, ensure the application runs in the foreground (do not let it daemonize itself). If it daemonizes, change `Type=forking`.

Logs aren't showing up: By default, stdout/stderr go to the journal. Check logs using `journalctl -u myapp.service -f`.

Frequently Asked Questions

Why should I use DynamicUser=yes?
DynamicUser allocates a unique, ephemeral user and group for your service each time it starts. This means the service cannot permanently own files on the filesystem and has zero access to other users' data, providing excellent isolation without the hassle of manually creating service accounts.
What is the difference between simple, exec, and forking types?
Type=simple assumes the service is ready immediately when the main process runs. Type=exec waits for the initial execve() to succeed. Type=forking is used for traditional daemons that fork a background process and exit the parent process (requires PIDFile).
How do I prevent a service from rapidly restarting and failing?
Use StartLimitBurst and StartLimitIntervalSec. For example, setting Burst=5 and Interval=60s means if the service crashes and restarts 5 times within 60 seconds, systemd will stop trying to restart it, preventing CPU exhaustion.
What does NoNewPrivileges=yes do?
It ensures that the service and any of its child processes can never gain new privileges through setuid/setgid executables. This is a crucial security hardening step that should almost always be enabled.
How do I use a systemd .timer instead of cron?
A systemd timer acts similarly to cron but is managed by systemd, providing integrated logging (journalctl) and better dependency management. Create a .timer file with an OnCalendar expression and link it to a .service file of the same name.