🐧 Linux - Services
On Unix-based systems, init
is the first process started during booting and continues running until the system is shut down. init
also handles starting up other programs and services on reboot.
There are currently three ways to control programs that act as services:
- System-V init: The original Unix-way. Doesn't allow parallelism or specifying an order in which the programs are started, which makes startup slow and make some multi-service configurations tedious.
upstart
(2006): System-V init replacement originally developed for Ubuntu but works on most other Linux distributions. But even Ubuntu has started switching tosystemd
as of 2014 because Ubuntu's parent distribution Debian voted to switch tosystemd
. You can easily replaceupstart
withsystemd
on Ubuntu 14.10 and newer.systemd
(2010): The current de-facto System-V init replacement enabled by default on all the major Linux distributions, except Debian and Ubuntu where the switch is still underway.
# Preferred usage order:
systemd > upstart > System-v init
The most important rule. If you don't know what a service is or does, don't disable it. This can seriously cripple your system.
Test before overriding. When you configure a totally new boot up sequence, try it for one boot before totally discarding the previous configuration e.g. by using grub.
System-V init
# Temporarily enabling or disabling services
/etc/init.d/mysql # help
/etc/init.d/mysql stop
/etc/init.d/mysql start
/etc/init.d/mysql status
/etc/init.d/mysql restart
System-V init scripts are in /etc/init.d
and enabled using update-rc.d
.
# Create a service
cp my-application-script /etc/init.d/my-application
chmod a+x /etc/init.d/my-application
# Enable a service
update-rc.d my-application defaults
/etc/init.d/my-application start
# Disable a service
update-rc.d my-application disable
/etc/init.d/my-application stop
upstart
# Temporarily enabling or disabling services.
# First tries upstart, then System-V inits.
service mysql # help
service mysql stop
service mysql start
service mysql status
service mysql restart
upstart
scripts are in /etc/init
with .conf
suffix e.g. /etc/init/mysql.conf
.
# Create a service
cp my-application-script.conf /etc/init.d/my-application.conf
chmod a+x /etc/init/my-application.conf
# Disable a service
echo manual | sudo tee /etc/init/my-application.override
service my-application stop
# Enable a service
sudo rm /etc/init/my-application.override
service my-application start
# Check if a service is using upstart or System-V init.
status apache2
# Unkown job => not upstart, otherwise prints process id
Ubuntu
Installing systemd
while keeping upstart
on Ubuntu:
apt-get install systemd
Replacing upstart
with systemd
on Ubuntu.
# Ubuntu 14.10 and newer
apt-get install systemd-sysv # replacing upstart with systemd
# Try booting using grub before making change permanent.
# Add `init=/lib/systemd/systemd` on the line starting with `linux`.
linux ... quiet init=/lib/systemd/systemd
# Activate permanent booting with systemd.
# In /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet init=/lib/systemd/systemd"