Startup and init systems (systemd) in Linux CLI - Time & Space Complexity
When a Linux system starts, systemd runs many tasks to get everything ready. Understanding how the time to start grows helps us see what slows down boot.
We ask: How does the startup time change as the number of services increases?
Analyze the time complexity of starting multiple systemd services sequentially.
for service in $(systemctl list-units --type=service --state=enabled --no-legend | awk '{print $1}'); do
systemctl start "$service"
systemctl is-active "$service"
sleep 1
done
This script starts each enabled service one by one, checks if it is active, then waits a second before the next.
Look at what repeats as the script runs.
- Primary operation: Starting and checking each service.
- How many times: Once per enabled service, so as many times as services exist.
As the number of services grows, the total time grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 starts and checks |
| 100 | About 100 starts and checks |
| 1000 | About 1000 starts and checks |
Pattern observation: The work grows directly with the number of services; double the services, double the work.
Time Complexity: O(n)
This means the total startup time grows in a straight line with the number of services started.
[X] Wrong: "Starting services all at once means the time stays the same no matter how many services there are."
[OK] Correct: Even if started in parallel, each service still needs time to initialize, so total resource use and time still grow with more services.
Knowing how startup time grows helps you understand system performance and troubleshoot slow boots. This skill shows you can think about real system behavior clearly.
"What if we started services in parallel instead of one by one? How would the time complexity change?"