In Linux, systemd I need to add new target that will be responsible for shuting down a group of my own, previously added services. All my systemd units have prefix "my", so let's say that I currently have three services:
my-svc1.servicemy-svc2.servicemy-svc3.service
and I would like the new target be called my-shutdown.target.
How should I implement this solution? I imagine that apart from creating the targer ``my-shutdown.targetI will probably need to add new service, let's say: my-shutdown.service`, that would do the actual job, is this correct?
Could you propose the example implementation of the solution, with the detailed explanation, and also explain how the proposed solution compares to the default Linux shoutdown mechanism?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
#!/bin/bash
set -e
# Graceful shutdown of services
systemctl stop my-svc1.service
systemctl stop my-svc2.service
systemctl stop my-svc3.service
# Optional: Add any additional cleanup tasks
# For example, unmounting specific filesystems, killing specific processes, etc.
Defines a custom target that explicitly requires your specific services
The Conflicts=shutdown.target ensures this target doesn't interfere with system-wide shutdown
Shutdown Service (my-shutdown.service):
Uses Type=oneshot to run the shutdown script once
ExecStop specifies the custom shutdown script
RemainAfterExit=yes keeps the service unit active after the script runs
Custom Shutdown Script:
Provides a controlled, explicit shutdown of your specific services
Allows for additional custom cleanup tasks
Comparison with Default Linux Shutdown Mechanism
Default Shutdown Mechanism:
Uses systemd's standard shutdown targets (shutdown.target, poweroff.target, etc.)
Stops all services in a generic, system-wide manner
Doesn't provide fine-grained control for specific service groups
Custom Solution Advantages:
Provides explicit control over a specific group of services
Allows custom shutdown procedures
Can include additional cleanup or preparation steps
Isolates shutdown logic for your specific services
Usage
To use this custom shutdown target:
bash
# Activate the target
sudo systemctl start my-shutdown.target
# Stop services using the target
sudo systemctl stop my-shutdown.target
Potential Improvements
Add error handling in the shutdown script
Implement logging
Add timeout mechanisms for service shutdown
Caveats
Ensure the shutdown script handles potential errors
Test thoroughly to verify the shutdown process works as expected
This solution provides a flexible, customizable approach to managing the shutdown of a specific group of services while maintaining compatibility with systemd's standard shutdown mechanisms.
User
If I understand correctly usage of Requires instead of Wants in the my-shutdown.target or my-shutdown.service would mean that if any of my listed services would not be running, then all te remaining ones would not be shut down, is that right? I cannot guarantee that all my services are always running but I need my shutdown service to stop all my remaining services that are still running.
Assistant
You're correct about the behavior of Requires. Let me clarify and provide a more flexible solution:
Recommended Approach
Instead of Requires, use Wants in the target file. This allows the target to proceed even if some services are not running:
systemd
[Unit]
Description=Custom Shutdown Target for My Services
Wants=my-svc1.service my-svc2.service my-svc3.service
Improved Shutdown Script
Here's a more robust shutdown script that handles services dynamically:
bash
#!/bin/bash
set -e
# Array of services to potentially stop
SERVICES=(
"my-svc1.service"
"my-svc2.service"
"my-svc3.service"
)
# Function to check and stop running services
stop_running_services() {
for service in "${SERVICES[@]}"; do
# Check if the service is active
if systemctl is-active --quiet "$service"; then
echo "Stopping $service"
systemctl stop "$service"
else
echo "$service is not running"
fi
}
}
# Main shutdown procedure
main() {
echo "Starting custom shutdown procedure"
stop_running_services
echo "Custom shutdown procedure completed"
}
# Run the main function
main
Key Differences and Improvements
Flexible Service Handling:
Uses Wants instead of Requires
Script checks and stops only running services
Prevents shutdown failure if some services are already stopped
Dynamic Service Management:
Checks each service's status before attempting to stop
Provides logging of actions
Continues processing even if some services are not running
Systemd Service File Adjustment
systemd
[Unit]
Description=Custom Shutdown Procedure for My Services
Wants=my-svc1.service my-svc2.service my-svc3.service
[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/local/bin/my-custom-shutdown.sh
RemainAfterExit=yes
[Install]
WantedBy=my-shutdown.target
Additional Considerations
The script uses systemctl is-active --quiet to silently check service status
It will attempt to stop all specified services
Provides clear logging of actions
Fails gracefully if services are in unexpected states
Best Practices
Always test the shutdown script in a controlled environment
Add error logging if needed
Consider adding timeout mechanisms for service stoppage
Ensure the script has proper permissions and is executable
This approach provides a robust, flexible solution for shutting down a group of services, handling various runtime scenarios where not all services may be running simultaneously.