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.
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.
- Start: This command initializes and starts the service.
- Stop: This command stops the service.
- Restart: This command stops and then starts the service.
- Reload: This command reloads the service's configuration without stopping it.
- Status: This command checks the status of the service.
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
- 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.
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
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
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.
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
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.