You're on the right track thinking about needing both a target and a service. Let me show you a few different approaches depending on your exact use case.
Approach 1: Manual Shutdown Target (Most Common)
If you want a target you can manually activate to stop your services:
my-shutdown.target:
[Unit]
Description=Shutdown Target for My Services
Wants=my-shutdown.service
After=my-shutdown.service
[Install]
WantedBy=multi-user.target
my-shutdown.service:
[Unit]
Description=Shutdown My Services
After=my-svc1.service my-svc2.service my-svc3.service
[Service]
Type=oneshot
ExecStart=/bin/systemctl stop my-svc1.service my-svc2.service my-svc3.service
RemainAfterExit=no
TimeoutStartSec=30
[Install]
WantedBy=my-shutdown.target
Usage: systemctl start my-shutdown.target
Approach 2: Integrated with System Shutdown
If you want your services to be stopped during system shutdown in a controlled manner:
my-shutdown.target:
[Unit]
Description=Shutdown Target for My Services
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target poweroff.target
Conflicts=my-svc1.service my-svc2.service my-svc3.service
After=my-svc1.service my-svc2.service my-svc3.service
[Install]
WantedBy=shutdown.target
With this approach, you don't need a separate service - the Conflicts= directive ensures that when my-shutdown.target starts (during system shutdown), it will stop the conflicting services.
Approach 3: Service Group Management
For ongoing management, you could also modify your existing services to be part of a group:
my-services.target:
[Unit]
Description=My Services Group Target
Wants=my-svc1.service my-svc2.service my-svc3.service
After=my-svc1.service my-svc2.service my-svc3.service
[Install]
WantedBy=multi-user.target
Then modify each service to include:
[Install]
WantedBy=my-services.target
This way you can manage them as a group: systemctl stop my-services.target stops all services.
Implementation Steps
- Create the target file in
/etc/systemd/system/my-shutdown.target
- If using Approach 1, create
/etc/systemd/system/my-shutdown.service
- Reload systemd:
systemctl daemon-reload
- Enable the target:
systemctl enable my-shutdown.target
Comparison to Default Linux Shutdown
Default Linux Shutdown:
- Uses a hierarchy of targets:
poweroff.target → shutdown.target → sysinit.target
- Systemd automatically determines service stop order based on dependencies
- Services are stopped in reverse order of their startup dependencies
- Built-in timeout handling and failure recovery
Your Custom Solution:
-
Advantages:
- Granular control over specific service groups
- Can be triggered independently of system shutdown
- Predictable order of operations
- Can include custom logic in the shutdown service
-
Considerations:
- Manual maintenance required when adding/removing services
- Less automatic dependency resolution
- Need to handle timeouts and failures explicitly
Best Practices:
- Use Approach 1 for manual service group management
- Use Approach 2 if you need integration with system shutdown sequence
- Consider setting appropriate
TimeoutStopSec values in your services
- Test the shutdown sequence thoroughly, especially under load
The custom target approach gives you fine-grained control while still leveraging systemd's robust service management capabilities. It's particularly useful for application stacks that need coordinated shutdown procedures.