📖 Scenario: You are managing a Linux server and want to create a simple service that runs a custom script at startup using systemd. This will help you automate tasks when the system boots.
🎯 Goal: Create a basic systemd service file to run a script at startup, enable the service, and check its status.
📋 What You'll Learn
Create a script file with a simple echo command
Create a systemd service file with correct sections and directives
Enable the service to start at boot
Start the service manually
Check the service status
💡 Why This Matters
🌍 Real World
Automating tasks on Linux servers or desktops to run scripts or programs at system startup.
💼 Career
System administrators and DevOps engineers often create and manage systemd services to control software and scripts on Linux systems.
Progress0 / 4 steps
1
Create the startup script
Create a file called /usr/local/bin/startup_script.sh with the content #!/bin/bash on the first line and echo "Startup script running" on the second line. Make sure the script is executable by running chmod +x /usr/local/bin/startup_script.sh.
Linux CLI
Hint
Use a text editor like nano or vim to create the file, then run chmod +x /usr/local/bin/startup_script.sh to make it executable.
2
Create the systemd service file
Create a file called /etc/systemd/system/startup_script.service with the following content: under the [Unit] section, add Description=Startup Script Service; under the [Service] section, add ExecStart=/usr/local/bin/startup_script.sh; under the [Install] section, add WantedBy=multi-user.target.
Linux CLI
Hint
Use a text editor to create the service file with the exact sections and lines as described.
3
Enable and start the service
Run the commands systemctl enable startup_script.service to enable the service at boot and systemctl start startup_script.service to start the service immediately.
Linux CLI
Hint
Use systemctl enable to set the service to run at boot and systemctl start to run it now.
4
Check the service status
Run the command systemctl status startup_script.service and observe the output. The output should include the line Active: active (running) indicating the service is running.
Linux CLI
Hint
Use systemctl status startup_script.service to see if the service is running.
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
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 A
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
Step 1: Understand systemctl commands
To enable a service to start at boot, the command is systemctl enable [service].
Step 2: Match command to action
systemctl enable nginx uses enable with nginx, which is correct for auto-start.
Final Answer:
systemctl enable nginx -> Option A
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
Step 1: Understand systemctl is-active
This command checks if a service is running and returns its state.
Step 2: Identify expected output when running
If the SSH service is running, the output is active.
Final Answer:
active -> Option C
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
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 D
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
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
Run systemctl enable yourservice to 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 B
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 unknownstrongwhile earning +1 Grip toward your certificate.