A convenient ZSH script that simplifies the creation of systemd service and timer units from command line instructions.
The Systemd Unit Maker script allows you to easily create systemd service and timer units with minimal manual editing. It handles all the boilerplate configuration, provides a straightforward command-line interface for defining your services, and allows you to fine-tune the units before installation.
- Systemd
- ZSH shell
- Text editor (nano by default, nvim if available)
- Appropriate permissions (root for system-wide units)
- Clone this repository or download the script and template files
- Ensure the script has executable permissions:
chmod +x systemd_unit_maker.sh
- Make sure the templates directory is in the same directory as the script
./systemd_unit_maker.sh [--user|--system] --name UNIT_NAME --command "COMMAND" \
[--description "DESCRIPTION"] [--template "TEMPLATE"] \
[--start] [--enable] [--no-timer]
--user
: Install for current user (default behavior)--system
: Install system-wide (requires root)--name
: Name for the systemd unit (required)--command
: Command to run in the service (required)--description
: Description of the service (optional)--template
: Template name to use (optional, default "default")--start
: Start the service after creation without enabling it (default: false)--enable
: Enable and start the timer after creation (default: false)--no-timer
: Do not create a timer unit, only create the service unit (default: false)
The script includes several templates:
default
: Standard systemd service/timerzsh
: Service that runs commands using ZSHzsh_boot
: Service that runs at boot time using ZSH
For boot templates (*_boot
), a timer is automatically created even without specifying a frequency or calendar.
After initial configuration, the script will open your editor (nano by default, nvim if available) to allow you to make final adjustments to the unit files before installation.
./systemd_unit_maker.sh --user --name backup_home \
--command "tar -czf /tmp/backup.tar.gz /home/user" \
--description "Daily home backup" \
--enable
sudo ./systemd_unit_maker.sh --system --name log_rotation \
--command "/usr/local/bin/rotate_logs.sh" \
--description "Hourly log rotation" \
--enable
./systemd_unit_maker.sh --user --name weekly_cleanup \
--command "/home/user/scripts/cleanup.sh" \
--description "Weekly temporary file cleanup"
./systemd_unit_maker.sh --user --name on_demand_service \
--command "notify-send 'Running on demand'" \
--description "On-demand service" \
--no-timer
./systemd_unit_maker.sh --user --name custom_service \
--command "/home/user/scripts/custom_script.sh" \
--description "Custom service using ZSH template" \
--template "zsh" \
--start
./systemd_unit_maker.sh --user --name startup_script \
--command "/home/user/scripts/startup.sh" \
--description "Run script at system boot" \
--template "zsh_boot" \
--enable
When a timer is created (which is the default unless --no-timer
is specified), the script will:
- Create a default timer unit file with common configurations commented out
- Open the timer file in your editor so you can customize the timing
- By default, a daily timer configuration is used for regular templates
You can customize the timer unit file during the interactive editing phase by uncommenting or modifying the timer settings:
OnBootSec=5min
: Run 5 minutes after boot (automatically used for boot templates)OnCalendar=*-*-* *:00:00
: Run every hourOnCalendar=*-*-* 02:00:00
: Run every day at 2 AMOnCalendar=Mon..Fri *-*-* 08:00:00
: Run on weekdays at 8 AMOnUnitActiveSec=1d
: Run daily after the last executionOnUnitActiveSec=1h
: Run hourly after the last executionAccuracySec=1min
: Set timer accuracy to 1 minute
For more complex timer expressions, refer to the systemd.timer documentation.
The script will open your preferred text editor (nano by default, nvim if available) to let you make final adjustments to the service and timer files before they are installed.
If the service or timer file already exists, the script will:
- Show a diff between the existing and new file
- Ask for confirmation before overwriting
- Allow you to skip overwriting specific files
When using templates with "boot" in their name (e.g., zsh_boot
), a timer will be created automatically with boot-specific settings. These timers are configured to run shortly after system boot and then once daily thereafter.
If you encounter issues:
-
Check the status of your service and timer:
systemctl --user status unit_name.service systemctl --user status unit_name.timer
-
View the logs for your service:
journalctl --user -u unit_name.service
-
Ensure your command works when run manually
-
Verify the systemd units were created in the correct location:
- User units:
~/.config/systemd/user/
- System units:
/etc/systemd/system/
- User units:
-
Check for syntax errors in your service or timer file:
systemd-analyze verify unit_name.service systemd-analyze verify unit_name.timer