Linux System Administration

0 of 85 lessons complete (0%)

Lab

Lab 3: Startup and Shutdown Script

You don’t have access to this lesson

Please register or sign in to access the course content.

Lab 3: Startup & Shutdown Scripts

Objective:

  • To understand and practice the creation and management of startup and shutdown scripts on a Linux system.

Topics Covered:

  1. Understanding Runlevels
  2. Creating Custom Startup Scripts
  3. Managing Service Startup and Shutdown
  4. Utilizing Cron Jobs for Scheduled Tasks

Lab Exercises:

Exercise 1: Understanding Runlevels

  1. Explore the concept of runlevels on the Linux system.
  2. Identify the default runlevel on your system.
  3. Change the default runlevel temporarily and observe the changes.
  4. Discuss the significance of different runlevels.
# Explore the concept of runlevels on the Linux system.
runlevel

# Identify the default runlevel on your system.
ls -l /etc/systemd/system/default.target

# Change the default runlevel temporarily and observe the changes.
sudo systemctl isolate multi-user.target

# Return to the default runlevel
sudo systemctl isolate graphical.target

Exercise 2: Creating Custom Startup Scripts

  1. Create a simple bash script that prints a message to a file during system startup.
  2. Configure the script to run at the system’s default runlevel.
  3. Test the script by rebooting the system and checking the output file.
# Create a simple bash script that prints a message to a file during system startup.
echo 'echo "Hello from custom startup script" >> /tmp/startup_message.txt' > custom_startup.sh

# Configure the script to run at the system's default runlevel.
sudo mv custom_startup.sh /etc/init.d/
sudo chmod +x /etc/init.d/custom_startup.sh
sudo update-rc.d custom_startup.sh defaults

# Test the script by rebooting the system and checking the output file.
sudo reboot
cat /tmp/startup_message.txt

Exercise 3: Managing Service Startup and Shutdown

  1. Identify a service that starts automatically during system boot.
  2. Disable the auto-start feature for the chosen service.
  3. Create a custom service startup script for a fictitious service.
  4. Test the custom service startup script by rebooting the system.
# Identify a service that starts automatically during system boot.
systemctl list-unit-files --type=service

# Disable the auto-start feature for the chosen service.
sudo systemctl disable <service_name>

# Create a custom service startup script for a fictitious service.
# Example: Create /etc/systemd/system/custom_service.service
# [Unit]
# Description=Custom service

# [Service]
# ExecStart=/path/to/custom_script.sh

# [Install]
# WantedBy=default.target

# Test the custom service startup script by rebooting the system.
sudo systemctl enable custom_service
sudo systemctl start custom_service

Exercise 4: Utilizing Cron Jobs for Scheduled Tasks

  1. Create a bash script that performs a simple task (e.g., creating a timestamped file).
  2. Schedule the script to run every day at a specific time using the cron job scheduler.
  3. Verify that the cron job is running as expected.
# Create a bash script that performs a simple task.
echo 'echo "Timestamp: $(date)" > /tmp/scheduled_task.txt' > scheduled_task.sh
chmod +x scheduled_task.sh

# Schedule the script to run every day at a specific time using the cron job scheduler.
crontab -e
# Add the line: 0 2 * * * /path/to/scheduled_task.sh

# Verify that the cron job is running as expected.
cat /tmp/scheduled_task.txt

Lab Documentation:

Provide detailed instructions for each exercise, including command syntax and expected outcomes. Include explanations of the purpose behind each task.