/etc/init.d/

The /etc/init.d/ directory is a key component of the traditional SysVinit system, housing the initialization scripts for managing services on a Linux system. These scripts control the starting, stopping, restarting, and checking the status of services. Each script typically corresponds to a specific service, such as apache2, mysql, or ssh.

Understanding /etc/init.d/

Directory Structure

The /etc/init.d/ directory contains a collection of scripts that are used to control services. These scripts are invoked by the init system at boot, shutdown, and when changing runlevels.

Example Scripts

Here’s a partial listing of what the /etc/init.d/ directory might contain:

/etc/init.d/
├── apache2
├── cron
├── networking
├── ssh
└── mysql

Common Operations with Init Scripts

Starting a Service

To start a service, use the start command:

sudo /etc/init.d/apache2 start

Stopping a Service

To stop a service, use the stop command:

sudo /etc/init.d/apache2 stop

Restarting a Service

To restart a service, use the restart command:

sudo /etc/init.d/apache2 restart

Checking the Status of a Service

To check the status of a service, use the status command:

sudo /etc/init.d/apache2 status

Anatomy of an Init Script

Init scripts are typically written in shell script and follow a standard structure. Here’s a simplified example of an init script for a hypothetical service:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          example-service
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example service
# Description:       This is a simple example of an init script
### END INIT INFO

# Source function library.
. /lib/lsb/init-functions

case "$1" in
  start)
    log_daemon_msg "Starting example service" "example-service"
    # Commands to start service
    log_end_msg $?
    ;;
  stop)
    log_daemon_msg "Stopping example service" "example-service"
    # Commands to stop service
    log_end_msg $?
    ;;
  restart)
    log_daemon_msg "Restarting example service" "example-service"
    $0 stop
    $0 start
    ;;
  status)
    # Commands to check status of service
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0

Explanation of Key Sections

  • Shebang (#!/bin/sh): Indicates that the script should be run using the /bin/sh shell.

  • INIT INFO Block: Metadata for the init system, providing information about the script's dependencies, start and stop priorities, and descriptions.

  • Function Library: Sourcing the /lib/lsb/init-functions library provides functions like log_daemon_msg and log_end_msg for logging service actions.

  • Case Statement: Handles different actions (start, stop, restart, status) based on the argument passed to the script.

Enabling and Disabling Services

To enable a service to start automatically at boot, create symbolic links in the appropriate runlevel directories (/etc/rc.d/rc?.d/).

Enable a Service

Using the update-rc.d command:

sudo update-rc.d apache2 defaults

This creates the necessary symlinks for the apache2 service to start at the default runlevels.

Disable a Service

To disable a service:

sudo update-rc.d apache2 remove

This removes the symlinks, preventing the service from starting at boot.

Migrating to systemd

With many modern Linux distributions now using systemd, services are often managed through systemd unit files rather than init scripts. However, backward compatibility allows many init scripts to be invoked by systemd.

To convert an init script to a systemd service unit, you typically create a unit file in /etc/systemd/system/. Here’s an example conversion for apache2:

Example Systemd Unit File for Apache2

[Unit]
Description=The Apache HTTP Server
After=network.target

[Service]
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl stop
ExecReload=/usr/sbin/apachectl graceful
Type=forking

[Install]
WantedBy=multi-user.target
  • [Unit]: Describes the service and its dependencies.

  • [Service]: Defines how the service should be started, stopped, and reloaded.

  • [Install]: Specifies how the service should be enabled or linked to targets.

Conclusion

The /etc/init.d/ directory and its scripts are fundamental to the SysVinit system, providing control over system services. Understanding how to use and write these scripts is essential for managing services on systems that still use SysVinit or for understanding legacy systems. Transitioning to systemd simplifies and unifies service management with more advanced features and capabilities.

If you have more specific questions or need further examples, feel free to ask!The /etc/init.d/ directory is a key component of the traditional SysVinit system, housing the initialization scripts for managing services on a Linux system. These scripts control the starting, stopping, restarting, and checking the status of services. Each script typically corresponds to a specific service, such as apache2, mysql, or ssh.

Understanding /etc/init.d/

Directory Structure

The /etc/init.d/ directory contains a collection of scripts that are used to control services. These scripts are invoked by the init system at boot, shutdown, and when changing runlevels.

Example Scripts

Here’s a partial listing of what the /etc/init.d/ directory might contain:

/etc/init.d/
├── apache2
├── cron
├── networking
├── ssh
└── mysql

Common Operations with Init Scripts

Starting a Service

To start a service, use the start command:

sudo /etc/init.d/apache2 start

Stopping a Service

To stop a service, use the stop command:

sudo /etc/init.d/apache2 stop

Restarting a Service

To restart a service, use the restart command:

sudo /etc/init.d/apache2 restart

Checking the Status of a Service

To check the status of a service, use the status command:

sudo /etc/init.d/apache2 status

Anatomy of an Init Script

Init scripts are typically written in shell script and follow a standard structure. Here’s a simplified example of an init script for a hypothetical service:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          example-service
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example service
# Description:       This is a simple example of an init script
### END INIT INFO

# Source function library.
. /lib/lsb/init-functions

case "$1" in
  start)
    log_daemon_msg "Starting example service" "example-service"
    # Commands to start service
    log_end_msg $?
    ;;
  stop)
    log_daemon_msg "Stopping example service" "example-service"
    # Commands to stop service
    log_end_msg $?
    ;;
  restart)
    log_daemon_msg "Restarting example service" "example-service"
    $0 stop
    $0 start
    ;;
  status)
    # Commands to check status of service
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0

Explanation of Key Sections

  • Shebang (#!/bin/sh): Indicates that the script should be run using the /bin/sh shell.

  • INIT INFO Block: Metadata for the init system, providing information about the script's dependencies, start and stop priorities, and descriptions.

  • Function Library: Sourcing the /lib/lsb/init-functions library provides functions like log_daemon_msg and log_end_msg for logging service actions.

  • Case Statement: Handles different actions (start, stop, restart, status) based on the argument passed to the script.

Enabling and Disabling Services

To enable a service to start automatically at boot, create symbolic links in the appropriate runlevel directories (/etc/rc.d/rc?.d/).

Enable a Service

Using the update-rc.d command:

sudo update-rc.d apache2 defaults

This creates the necessary symlinks for the apache2 service to start at the default runlevels.

Disable a Service

To disable a service:

sudo update-rc.d apache2 remove

This removes the symlinks, preventing the service from starting at boot.

Migrating to systemd

With many modern Linux distributions now using systemd, services are often managed through systemd unit files rather than init scripts. However, backward compatibility allows many init scripts to be invoked by systemd.

To convert an init script to a systemd service unit, you typically create a unit file in /etc/systemd/system/. Here’s an example conversion for apache2:

Example Systemd Unit File for Apache2

[Unit]
Description=The Apache HTTP Server
After=network.target

[Service]
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl stop
ExecReload=/usr/sbin/apachectl graceful
Type=forking

[Install]
WantedBy=multi-user.target
  • [Unit]: Describes the service and its dependencies.

  • [Service]: Defines how the service should be started, stopped, and reloaded.

  • [Install]: Specifies how the service should be enabled or linked to targets.

Conclusion

The /etc/init.d/ directory and its scripts are fundamental to the SysVinit system, providing control over system services. Understanding how to use and write these scripts is essential for managing services on systems that still use SysVinit or for understanding legacy systems. Transitioning to systemd simplifies and unifies service management with more advanced features and capabilities.

Last updated