How to Customize systemd Service Restart Behavior Without Modifying Upstream Unit Files


2 views

When managing services through systemd, we often need to modify service behaviors like restart policies. However, directly editing upstream unit files (typically located in /lib/systemd/system/) is bad practice because:

  • Package updates may overwrite your changes
  • It violates the principle of maintaining clean package management
  • Makes system maintenance harder across multiple servers

The proper way to customize unit files is using systemd's "drop-in" directory feature. Here's how to implement it for the pimd service:

# Create the service-specific drop-in directory
sudo mkdir -p /etc/systemd/system/pimd.service.d/

# Create a configuration override file
sudo tee /etc/systemd/system/pimd.service.d/restart.conf <<EOF
[Service]
Restart=always
RestartSec=5s
EOF

# Reload systemd to apply changes
sudo systemctl daemon-reload

After applying changes, verify they took effect:

# Check the effective unit configuration
systemctl show pimd --property=Restart

# View merged unit configuration
systemd-analyze cat-config systemd/pimd.service

The drop-in method works for various service modifications:

# Example 1: Memory limits
[Service]
MemoryLimit=512M

# Example 2: Environment variables
[Service]
Environment="LOG_LEVEL=debug"

# Example 3: Custom ExecStart
[Service]
ExecStart=
ExecStart=/usr/sbin/pimd --custom-flag
  • Use descriptive filenames (restart.conf, memory.conf)
  • Keep each drop-in file focused on one aspect
  • Document changes in the file with comments
  • Test changes with systemctl restart before enabling

If changes aren't applying:

  1. Verify directory permissions (must be readable by systemd)
  2. Check for syntax errors with systemd-analyze verify
  3. Ensure you ran systemctl daemon-reload

When managing services on Linux systems using systemd, we often encounter situations where we need to modify service behavior but want to preserve the original upstream unit files. Directly editing files in /lib/systemd/system/ is problematic because:

  1. Package updates may overwrite your changes
  2. It violates the principle of maintaining separation between vendor and local configurations
  3. Makes system maintenance and auditing more difficult

Systemd provides an elegant solution through its "drop-in" directory mechanism. Here's how to implement it for the pimd service:

# Create the override directory
sudo mkdir -p /etc/systemd/system/pimd.service.d

# Create the override file
sudo tee /etc/systemd/system/pimd.service.d/override.conf <<EOF
[Service]
Restart=always
EOF

# Reload systemd configuration
sudo systemctl daemon-reload

Beyond just restart behavior, you can customize many aspects of the service:

# Example of multiple customizations
sudo tee /etc/systemd/system/pimd.service.d/override.conf <<EOF
[Service]
Restart=always
RestartSec=5s
Environment="LOG_LEVEL=debug"
User=customuser
EOF

Always verify your changes took effect:

# Check merged unit configuration
systemctl show pimd --no-pager

# Specifically check the Restart setting
systemctl show pimd -p Restart --value

For more complex scenarios, you can:

  1. Create multiple drop-in files that load in alphabetical order
  2. Use systemd-analyze verify to check your configuration
  3. Mask the original unit file completely if needed

Remember that after any changes to unit files, you must run systemctl daemon-reload for the changes to take effect.