How to Control and Modify Linux Service Boot Sequence and Dependencies


3 views

In modern Linux systems using systemd (the majority of distributions today), services are managed through units defined in .service files. The boot sequence is controlled through dependencies and ordering directives in these unit files.

Several critical directives in systemd unit files control the boot sequence:


[Unit]
Description=My Custom Service
After=network.target postgresql.service
Requires=postgresql.service
Wants=network.target

Method 1: Editing Existing Service Files

Locate the service file (usually in /usr/lib/systemd/system/ or /etc/systemd/system/):


sudo systemctl edit --full servicename.service

Add or modify the After= and Before= directives to establish ordering.

Method 2: Creating Drop-In Overrides

For safer modifications that persist across updates:


sudo mkdir -p /etc/systemd/system/servicename.service.d/
sudo nano /etc/systemd/system/servicename.service.d/order.conf

Add content like:


[Unit]
After=other-service.service

Using systemd-analyze

Analyze current boot performance and sequence:


systemd-analyze critical-chain servicename.service
systemd-analyze plot > boot-sequence.svg

Managing Service Conflicts

When services shouldn't run simultaneously:


[Unit]
Conflicts=other-service.service

Making Nginx start only after MySQL is fully ready:


[Unit]
Description=NGINX HTTP Server
After=syslog.target network.target mysql.service
Requires=mysql.service

After making changes:


sudo systemctl daemon-reload
sudo systemctl restart servicename
systemd-analyze verify servicename.service

Check specific dependencies:


systemctl list-dependencies servicename.service

For systems using SysVinit (rare these days), modify scripts in /etc/init.d/ and update rc.d links:


update-rc.d servicename defaults 90 10

In Linux systems, services are typically managed by either Systemd (modern distributions) or SysVinit (older systems). The startup sequence is crucial for system stability and performance, especially when services have dependencies.

For systems using Systemd, you can control service order through unit file directives:

[Unit]
Description=My Custom Service
After=network.target mysql.service
Requires=mysql.service

Key directives:

  • After: Specifies services that should start before this one
  • Before: Specifies services that should start after this one
  • Requires: Hard dependency
  • Wants: Soft dependency

To modify an existing service without editing the original unit file:

sudo systemctl edit nginx.service

This creates an override file at /etc/systemd/system/nginx.service.d/override.conf where you can add your ordering directives.

For older systems using SysVinit, service order is controlled by runlevel directories and script numbering:

ls /etc/rc.d/rc3.d/
# Output shows S##service and K##service files

To change order, modify the numbers in the filenames (lower numbers execute first):

sudo mv /etc/rc.d/rc3.d/S55sshd /etc/rc.d/rc3.d/S25sshd

Use these commands to analyze service relationships:

systemctl list-dependencies nginx.service
systemd-analyze critical-chain nginx.service

For complex scenarios, consider creating custom targets:

[Unit]
Description=My Custom Target
Requires=multi-user.target
After=multi-user.target
AllowIsolate=yes

Then link services to this target instead of the default ones.

  • Always test changes in a development environment first
  • Document your modifications
  • Consider using configuration management tools for consistency
  • Monitor boot performance after changes with systemd-analyze