mkunit service

Create a systemd service unit.

mkunit service <name> [flags]

Description

The service command creates a .service unit file for running daemons, servers, or one-shot scripts. Supports all common service options including restart policies, environment variables, and security hardening.

Arguments

Argument Description
<name> Name of the service (without .service extension)

Flags

Flag Description Default
--exec <command> Command to execute (ExecStart)
--workdir <path> Working directory for the service Current directory
--description <text> Human-readable description Auto-generated
--type <type> Service type: simple, exec, forking, oneshot, notify, idle simple
--restart <policy> Restart policy: no, always, on-success, on-failure, on-abnormal, on-abort, on-watchdog on-failure
--restart-sec <seconds> Time to wait before restart 5
--user <username> Run service as this user (system services only)
--group <groupname> Run service as this group (system services only)
--env <KEY=value> Set environment variable (can be repeated)
--env-file <path> Load environment from file
--after <unit> Start after this unit (can be repeated) network.target
--requires <unit> Require this unit (can be repeated)
--wants <unit> Want this unit (can be repeated)
--wanted-by <target> Install to this target default.target
--hardening Enable security hardening options false
--system Create a system service (requires sudo) false
--install Install the unit file after creation false
--start Start the service after installation false
--enable Enable the service to start on boot false
--output <path> Write unit file to custom path
--dry-run Print unit file without writing false
--no-interactive Disable interactive prompts false

Examples

Basic service

mkunit service myapp --exec "./server"

With restart and working directory

mkunit service api \
  --exec "/usr/local/bin/api serve" \
  --workdir /var/lib/api \
  --restart always \
  --install

With environment variables

mkunit service worker \
  --exec "./worker" \
  --env "REDIS_URL=localhost:6379" \
  --env "WORKERS=4"

With environment file

mkunit service webapp \
  --exec "./webapp" \
  --env-file /etc/webapp/env \
  --install --start

With security hardening

mkunit service secure-app \
  --exec "./app" \
  --user www-data \
  --hardening \
  --install

One-shot service (runs once, not a daemon)

mkunit service backup \
  --exec "/usr/local/bin/backup.sh" \
  --type oneshot

System service (requires sudo)

sudo mkunit service nginx-proxy \
  --exec "/usr/bin/nginx" \
  --system \
  --user nginx \
  --install --enable

Preview without creating

mkunit service myapp --exec "./server" --dry-run

Full example with all options

mkunit service myapp \
  --exec "/opt/myapp/bin/server --config /etc/myapp/config.yaml" \
  --workdir /opt/myapp \
  --description "My Application Server" \
  --type simple \
  --restart on-failure \
  --restart-sec 10 \
  --env "NODE_ENV=production" \
  --env "PORT=3000" \
  --after network.target \
  --after postgresql.service \
  --wants postgresql.service \
  --hardening \
  --install --enable --start

See Also