Bird
Raised Fist0
Linux CLIscripting~5 mins

Startup and init systems (systemd) in Linux CLI - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Startup and init systems (systemd)
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of services grows, the total time grows too.

Input Size (n)Approx. Operations
10About 10 starts and checks
100About 100 starts and checks
1000About 1000 starts and checks

Pattern observation: The work grows directly with the number of services; double the services, double the work.

Final Time Complexity

Time Complexity: O(n)

This means the total startup time grows in a straight line with the number of services started.

Common Mistake

[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.

Interview Connect

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.

Self-Check

"What if we started services in parallel instead of one by one? How would the time complexity change?"

Practice

(1/5)
1. What is the main purpose of systemd in a Linux system?
easy
A. To manage how the system starts and stops programs automatically
B. To edit text files in the system
C. To compile software from source code
D. To monitor network traffic

Solution

  1. Step 1: Understand systemd's role

    systemd is a system and service manager that controls how programs start and stop on Linux.
  2. 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.
  3. Final Answer:

    To manage how the system starts and stops programs automatically -> Option A
  4. Quick Check:

    systemd manages startup and shutdown = B [OK]
Hint: Remember: systemd controls startup and shutdown of services [OK]
Common Mistakes:
  • Confusing systemd with text editors
  • Thinking systemd compiles code
  • Mixing systemd with network tools
2. Which of the following is the correct command to enable a service named nginx to start automatically at boot?
easy
A. systemctl enable nginx
B. systemctl start nginx
C. systemctl status nginx
D. systemctl stop nginx

Solution

  1. Step 1: Understand systemctl commands

    To enable a service to start at boot, the command is systemctl enable [service].
  2. Step 2: Match command to action

    systemctl enable nginx uses enable with nginx, which is correct for auto-start.
  3. Final Answer:

    systemctl enable nginx -> Option A
  4. Quick Check:

    Enable means auto-start = A [OK]
Hint: Enable means auto-start at boot, start means immediate run [OK]
Common Mistakes:
  • Using start instead of enable for auto-start
  • Confusing status with enable
  • Using stop instead of enable
3. What will be the output of the command systemctl is-active sshd if the SSH service is running?
medium
A. inactive
B. failed
C. active
D. unknown

Solution

  1. Step 1: Understand systemctl is-active

    This command checks if a service is running and returns its state.
  2. Step 2: Identify expected output when running

    If the SSH service is running, the output is active.
  3. Final Answer:

    active -> Option C
  4. Quick Check:

    Running service status = active [OK]
Hint: Active means running, inactive means stopped [OK]
Common Mistakes:
  • Confusing active with inactive
  • Expecting failed when service is running
  • Thinking unknown means running
4. You run systemctl enable apache2 but the service does not start on boot. What is the most likely reason?
medium
A. The service is already running
B. You used start instead of enable
C. The systemctl command is not installed
D. The service file for apache2 is missing or incorrect

Solution

  1. Step 1: Understand what enable does

    Enable creates links so the service starts at boot but does not start it immediately.
  2. 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.
  3. Final Answer:

    The service file for apache2 is missing or incorrect -> Option D
  4. Quick Check:

    Missing service file blocks enable = D [OK]
Hint: Enable needs a valid service file to work [OK]
Common Mistakes:
  • Confusing enable with start
  • Assuming systemctl is missing
  • Thinking running service means enable failed
5. You want to create a custom service that runs a script at startup. Which two steps must you perform to make sure it starts automatically with systemd?
hard
A. Add your script to /etc/rc.local and make it executable
B. Create a service file in /etc/systemd/system, then run 'systemctl enable yourservice' to auto-start it
C. Place your script in /usr/bin and reboot the system
D. Run 'systemctl start yourservice' and then 'systemctl status yourservice' only

Solution

  1. 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.
  2. Step 2: Enable the service

    Run systemctl enable yourservice to make it start automatically at boot.
  3. Final Answer:

    Create a service file in /etc/systemd/system, then run 'systemctl enable yourservice' to auto-start it -> Option B
  4. Quick Check:

    Create service file + enable = auto-start [OK]
Hint: Create service file and enable it to auto-start [OK]
Common Mistakes:
  • Only starting service without enabling
  • Using old /etc/rc.local method
  • Placing script without service file
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard