Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Created June 30, 2024 02:38
Show Gist options
  • Save Cdaprod/d8489620ea6cdbdd7d168ca5589d18d4 to your computer and use it in GitHub Desktop.
Save Cdaprod/d8489620ea6cdbdd7d168ca5589d18d4 to your computer and use it in GitHub Desktop.

Understanding Init Scripts in Linux

What Are Init Scripts?

Init scripts, also known as initialization scripts, are shell scripts used in Unix-based systems to control the startup and shutdown of services. These scripts are typically located in the /etc/init.d directory and are executed by the init system (such as sysvinit, upstart, or systemd) during the boot and shutdown processes.

Purpose of Init Scripts

The primary purpose of init scripts is to manage the state of system services. They perform tasks such as starting, stopping, restarting, and reloading services. Each init script contains instructions for managing a specific service.

Key Functions of Init Scripts

  1. Start: This command initializes and starts the service.
  2. Stop: This command stops the service.
  3. Restart: This command stops and then starts the service.
  4. Reload: This command reloads the service's configuration without stopping it.
  5. Status: This command checks the status of the service.

Structure of Init Scripts

A typical init script includes a set of standardized functions and comments that describe the script's behavior. Here’s an example of a basic init script for a hypothetical service called myservice:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          myservice
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example init script for myservice
# Description:       This script starts and stops the myservice daemon.
### END INIT INFO

case "$1" in
  start)
    echo "Starting myservice"
    /usr/bin/myservice &
    ;;
  stop)
    echo "Stopping myservice"
    pkill myservice
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  status)
    if pgrep myservice > /dev/null
    then
      echo "myservice is running"
    else
      echo "myservice is stopped"
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0

Explanation

  • Shebang (#!/bin/bash): Specifies the script interpreter.
  • INIT INFO Block: Provides metadata about the script, such as the services it provides, dependencies, and run levels.
  • Case Statement: Handles different commands (start, stop, restart, status) and performs corresponding actions.

Managing Init Scripts

Adding Init Scripts

To add an init script, you typically copy it to the /etc/init.d directory and make it executable:

sudo cp myservice /etc/init.d/myservice
sudo chmod +x /etc/init.d/myservice

Enabling/Disabling Init Scripts

To enable an init script to run at startup and shutdown, use the update-rc.d command:

sudo update-rc.d myservice defaults

To disable it:

sudo update-rc.d -f myservice remove

Modern Alternatives: Systemd

Many modern Linux distributions have adopted systemd as their init system, which uses unit files instead of traditional init scripts. While init scripts are still supported, understanding systemd unit files is essential for managing services on these systems.

Example Systemd Unit File

Here’s an example of a simple systemd unit file for myservice:

[Unit]
Description=My Service

[Service]
ExecStart=/usr/bin/myservice
ExecStop=/bin/kill $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target

To use this unit file, you would typically save it as /etc/systemd/system/myservice.service and then enable and start the service using systemctl:

sudo systemctl enable myservice
sudo systemctl start myservice

Conclusion

Init scripts are a fundamental part of Unix-based systems, providing a way to manage services during the boot and shutdown processes. While systemd has become the standard for many distributions, understanding traditional init scripts remains valuable for managing legacy systems and services.

For more detailed information and best practices, refer to the official documentation of your Linux distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment