Skip to Content
Bash & LinuxSystemd Service

Systemd Service

Template for creating a systemd service unit that runs a script at boot.

Setup

  1. Create a .service file in /etc/systemd/system/:
[Unit] Description=Run Azure DevOps backup script at startup After=network-online.target Wants=network-online.target [Service] Type=oneshot User=sean Group=sean WorkingDirectory=/home/sean/devops-backup/ Environment=AZURE_DEVOPS_PAT=<PAT> Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ExecStart=/home/sean/devops-backup/backup-azure-devops.sh StandardOutput=append:/var/log/myscript.log StandardError=append:/var/log/myscript.log [Install] WantedBy=multi-user.target
  1. Enable and start:
sudo systemctl daemon-reload sudo systemctl enable your-service.service sudo systemctl start your-service.service

Key fields

  • Type=oneshot — Script runs once and exits (not a daemon)
  • After=network-online.target — Waits for network before running
  • Environment — Set environment variables the script needs
  • StandardOutput/StandardError — Redirect logs to a file
  • WantedBy=multi-user.target — Runs at normal boot (multi-user mode)
Last updated on