In this howto, you’ll learn the very minimum needed commands you’d have to know in order to manage your system’s startup processes (services). Whether it was Systemd, SysV init, Upstart, runit or OpenRC.
First, please note that:
- Whenever you see
{SERVICE_NAME}
it’s a variable and meant to be replaced with actual service name, likesshd
,apache2
,iptables
.. etc. - Enabling a service, it means making a service automatically start at the boot process. And disabling a service means, not to automatically start it at the boot process.
- All the following mentioned commands except for when listing the services or their status, are meant to be used as root or with
sudo
.
systemd is an init system used in Linux distributions to bootstrap the user space and manage all processes subsequently, instead of the UNIX System V or Berkeley Software Distribution (BSD) init systems. It is published as free and open-source software under the terms of the GNU Lesser General Public License (LGPL) version 2.1 or later.[5] The software has as one of its main goals the unification of basic Linux configurations and service behaviors across all distributions.
Source: Wikipedia.
To list all services:
$ systemctl list-unit-files
To list failed services:
$ systemctl --failed
To start a service:
$ systemctl start {SERVICE_NAME}
To restart a service:
$ systemctl restart {SERVICE_NAME}
To enable a service:
$ systemctl enable {SERVICE_NAME}
To list running services status:
$ systemctl list-units
To list available services:
$ systemctl --all
To stop a service:
$ systemctl stop {SERVICE_NAME}
To get the status of a service:
$ systemctl status {SERVICE_NAME}
To disable a service:
$ systemctl disable {SERVICE_NAME}
If your distro provides some tools to manage the services, it’s highly recommended to use them instead of manually creating/removing the symlinks whenever you want to enable or disable a service. RedHat and Fedora used to have service
and chkconfig
, Debian and its based distros like Ubuntu had service
and update-rc.d
.
To list services:
Using service
tool:
$ service --status-all
Note that, the output’s signs mean the following:
[ + ]
– Services with this sign are currently running.[ – ]
– Services with this sign are not currently running.[ ? ]
– Services that do not have a status switch.
Using chkconfig
tool:
$ chkconfig --all
Note that, you can even get more details on each {SERVICE_NAME}
(e.g: $ chkconfig {SERVICE_NAME} --list
).
Or, list them manually:
$ ls /etc/init.d/
To start a service:
Using service
tool:
$ service {SERVICE_NAME} start
Or, start it manually:
$ /etc/init.d/{SERVICE_NAME} start
To stop a service:
Using service
tool:
$ service {SERVICE_NAME} stop
Or, stop it manually:
$ /etc/init.d/{SERVICE_NAME} stop
To restart a service:
Using service
tool:
$ service {SERVICE_NAME} restart
Or, restart it manually:
$ /etc/init.d/{SERVICE_NAME} restart
To get the status of a service:
Using service
tool:
$ service {SERVICE_NAME} status
Or, get it manually:
$ /etc/init.d/{SERVICE_NAME} status
To enable a service:
Using update-rc.d
tool:
$ update-rc.d {SERVICE_NAME} defaults
Or using symlinks (manually):
$ cd /etc/rc3.d $ ln -s ../init.d/{SERVICE_NAME} S95{SERVICE_NAME}
Note that, the S95
is used to specify order. S01
will start before S02
, and so on.
You may use /etc/rc2.d
instead of /etc/rc3.d
.
Using chkconfig
tool:
$ chkconfig {SERVICE_NAME} on
And if you want to enable it on a specific runlevels, use the --level
switch. For example:
$ chkconfig {SERVICE_NAME} on --level 3
To disable a service:
Using update-rc.d
tool:
$ update-rc.d -f {SERVICE_NAME} remove
Note that, you will need to use the -f
switch if the applications /etc/init.d
start up file exists.
Or just remove the symlinks (manually):
$ rm /etc/rc3.d/*{SERVICE_NAME}
Or:
$ rm /etc/rc2.d/*{SERVICE_NAME}
Using chkconfig
tool:
$ chkconfig {SERVICE_NAME} off
And if you want to disable it on a specific runlevels, use the --level
switch. For example:
$ chkconfig {SERVICE_NAME} off --level 3
Upstart is an event-based replacement for the traditional init daemon – the method by which several Unix-like computer operating systems perform tasks when the computer is started. It was written by Scott James Remnant, a former employee of Canonical Ltd.
Source: Wikipedia
To list services:
$ initctl list
To start a service:
$ initctl start {SERVICE_NAME}
Or:
$ start {SERVICE_NAME}
To restart a service:
$ initctl restart {SERVICE_NAME}
Or:
$ restart {SERVICE_NAME}
To stop a service:
$ initctl stop {SERVICE_NAME}
Or:
$ stop {SERVICE_NAME}
To get the status of a service:
$ initctl status {SERVICE_NAME}
Or:
$ status {SERVICE_NAME}
To enable a service:
Usually, they’re enabled by default. But in case they’re not, or you’d like to change when they’re started and where they’re not, you’d need to modify the /etc/init/{SERVICE_NAME}.conf
. Then add or modify the lines that start with start on
and stop on
. For example:
$ start on runlevel [23]
$ stop on runlevel [!23]
To disable a service:
$ echo manual | sudo tee /etc/init/{SERVICE_NAME}.override
Note that, where the stanza manual
will stop Upstart from automatically loading the service on next boot. Any service with the .override
ending will take precedence over the original service file. You will only be able to start the service manually afterwards. If you do not want this then simply delete the .override
.
Or disable it manually. By modifying {SERVICE_NAME}.conf
which is located under /etc/init/
. Then comment out all lines that start with start on
.
But, if you want to completely disable it, so that it’s not even startable, do:
$ mv /etc/init/{SERVICE_NAME}.conf /etc/init/{SERVICE_NAME}.conf.disabled
runit is an init scheme for Unix-like operating systems that initializes, supervises, and ends processes throughout the operating system. Runit is a reimplementation of the daemontools process supervision toolkit that runs on the Linux, Mac OS X, *BSD, and Solaris operating systems. Runit features parallelization of the start up of system services, which can speed up the boot time of the operating system.
Runit is an init daemon, so it is the direct or indirect ancestor of all other processes. It is the first process started during booting, and continues running until the system is shut down.
Source: Wikipedia
To list services:
To list all available services:
$ ls /etc/sv/
To list all enabled services:
$ ls /var/service/
To start a service:
$ sv up {SERVICE_NAME}
Or:
$ sv u {SERVICE_NAME}
To restart a service:
$ sv restart {SERVICE_NAME}
Or:
$ sv t {SERVICE_NAME}
To stop a service:
$ sv down {SERVICE_NAME}
Or:
$ sv d {SERVICE_NAME}
To get the status of a service:
$ sv status {SERVICE_NAME}
Or:
$ sv s {SERVICE_NAME}
To get the current status of all enabled services:
$ sv status /var/service/*
To enable a service:
$ ln -s /etc/sv/service_name /var/service/
You can even prevent an enabled service from starting automatically at the boot process, by creating an empty file called down
under /etc/sv/{SERVICE_NAME}/
. For example:
$ touch /etc/sv/{SERVICE_NAME}/down
To undo that, simply remove the down
file. As the following:
$ rm -v /etc/sv/{SERVICE_NAME}/down
To disable a service:
$ rm /var/service/{SERVICE_NAME}
On Unix-like systems, OpenRC is a dependency-based init. Since 0.25 OpenRC includes openrc-init, which can replace /sbin/init.
OpenRC is the default init system of Gentoo, Alpine Linux and other Linux distributions, which means that the software packages and daemons of those distributions support it, coming with or using the available scripts. As well as Linux, OpenRC can also be used on several BSD systems. It was created by a NetBSD developer, who started the Gentoo/FreeBSD project. TrueOS, a FreeBSD based system is also using OpenRC as its default in its default configuration.
To list services:
$ rc-status
To list failed services:
$ rc-status --crashed
To list available services:
$ rc-update -v show
Or:
$ ls /etc/init.d/
To start a service:
$ rc-service {SERVICE_NAME} start
Or:
$ service {SERVICE_NAME} start
Or:
$ /etc/init.d/{SERVICE_NAME} start
To restart a service:
$ rc-service {SERVICE_NAME} restart
Or:
$ service {SERVICE_NAME} restart
Or:
$ /etc/init.d/{SERVICE_NAME} restart
To enable a service:
$ rc-update add {SERVICE_NAME}
To stop a service:
$ rc-service {SERVICE_NAME} stop
Or:
$ service {SERVICE_NAME} stop
Or:
$ /etc/init.d/{SERVICE_NAME} stop
To get the status of a service:
$ rc-service {SERVICE_NAME} status
Or:
$ service {SERVICE_NAME} status
Or:
$ /etc/init.d/{SERVICE_NAME} status
To disable a service:
$ rc-update del {SERVICE_NAME}