Simple Startup Script

Setting up a simple service with SystemD

Problem

Create a service in systemd that gets run once on startup.

Solution

1. Create Service File

[Unit]
Description=My Test Script
After=syslog.target
After=network.target

[Service]
Type=oneshot
User=root
Group=root
WorkingDirectory=/root
ExecStart=/path/to/my-script.sh
Environment=USER=root HOME=/root

[Install]
WantedBy=multi-user.target

The snippet above will run the /path/to/my-script.sh as the root user on system startup and will only run it once.

2. Reload services

This command will tell systemd to reload all the service files and detect the new file.

systemctl daemon-reload

3. Enable Service

And this command will enable the service, instructing systemd to run it at startup.

systemctl enable file-name-of-my-service.service