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?"
Practice
systemd in a Linux system?Solution
Step 1: Understand systemd's role
systemd is a system and service manager that controls how programs start and stop on Linux.Step 2: Compare options
Only To manage how the system starts and stops programs automatically describes managing startup and shutdown of programs, which matches systemd's purpose.Final Answer:
To manage how the system starts and stops programs automatically -> Option AQuick Check:
systemd manages startup and shutdown = B [OK]
- Confusing systemd with text editors
- Thinking systemd compiles code
- Mixing systemd with network tools
nginx to start automatically at boot?Solution
Step 1: Understand systemctl commands
To enable a service to start at boot, the command issystemctl enable [service].Step 2: Match command to action
systemctl enable nginx usesenablewithnginx, which is correct for auto-start.Final Answer:
systemctl enable nginx -> Option AQuick Check:
Enable means auto-start = A [OK]
- Using start instead of enable for auto-start
- Confusing status with enable
- Using stop instead of enable
systemctl is-active sshd if the SSH service is running?Solution
Step 1: Understand
This command checks if a service is running and returns its state.systemctl is-activeStep 2: Identify expected output when running
If the SSH service is running, the output isactive.Final Answer:
active -> Option CQuick Check:
Running service status = active [OK]
- Confusing active with inactive
- Expecting failed when service is running
- Thinking unknown means running
systemctl enable apache2 but the service does not start on boot. What is the most likely reason?Solution
Step 1: Understand what enable does
Enable creates links so the service starts at boot but does not start it immediately.Step 2: Identify why enable might fail
If the service file is missing or broken, enabling won't work properly, so it won't start on boot.Final Answer:
The service file for apache2 is missing or incorrect -> Option DQuick Check:
Missing service file blocks enable = D [OK]
- Confusing enable with start
- Assuming systemctl is missing
- Thinking running service means enable failed
Solution
Step 1: Create a systemd service file
Write a service file describing your script and place it in /etc/systemd/system/ to define how systemd runs it.Step 2: Enable the service
Runsystemctl enable yourserviceto make it start automatically at boot.Final Answer:
Create a service file in /etc/systemd/system, then run 'systemctl enable yourservice' to auto-start it -> Option BQuick Check:
Create service file + enable = auto-start [OK]
- Only starting service without enabling
- Using old /etc/rc.local method
- Placing script without service file
Ready to prove it?
Pass the test and this pattern moves unknown
