1. Overview
In this tutorial, we’ll be looking at the differences between systemctl and service commands in Linux.
2. Linux Initialization Process
On startup, Linux follows a set of pre-defined boot sequences. At the last step of the boot process, it runs an init system. The init system serves as the first process in the user space and is responsible for starting up other important services and processes. There are two most relevant init systems in Linux, namely System V init (SysVInit) and SystemD.
2.1. SysVInit and SystemD
SysVInit is the classic initialization process in Linux. The initialization process relies on the individual service to install relevant scripts on the /etc/init.d directory. Additionally, the scripts must support the standard commands such as start, stop, and status. One of the main characteristics of this init system is that it is a start-once process and does not track the individual services afterward. The service command is used for running these init scripts from the terminal.
SystemD, on the other hand, is a recent initialization system that aims to replace SysVInit. In fact, most Linux distributions such as Debian and Red Hat are already using SystemD as their init system out of the box. In contrast to SysVInit, SystemD continues to run as a daemon process after the initialization is completed. Additionally, they are also actively tracking the services through their cgroups. The systemctl command is the entry point for users to interact and configures the SystemD.
In short, the differences between service and systemctl commands can be summarized as two different commands for two different init systems.
3. service Command
The service command has a general syntax of:
$ service SCRIPT COMMAND
When invoked, the service command looks up the script to run at the path */etc/init.d/*SCRIPT. It then runs the script, passing the COMMAND unchanged as the arguments. We can, of course, run the script using its path directly, bypassing the service command. But, the service command guarantees a predictable running environment by removing most of the variables and setting the root path as the current working directory.
To put the commands into action, let’s install Nginx using the package manager on our Linux system:
$ sudo apt-get install -y nginx
Note that Nginx is chosen arbitrarily, and the examples will work for any software installation that comes with SysVInit scripts.
After the installation, we’ll see a shell script with the filename as nginx in the directory /etc/init.d. To start the process, we can use the service nginx start command:
$ sudo service nginx start
* Starting nginx nginx [ OK ]
Besides that, it also accepts the stop command to terminate the background Nginx process:
$ sudo service nginx stop
* Stopping nginx nginx [ OK ]
Additionally, we could also use the restart command to restart the Nginx process:
$ sudo service nginx restart
* Restarting nginx nginx [ OK ]
Finally, we can also check the status of the process with the status command:
$ sudo service nginx status
* nginx is running
4. systemctl Command
The systemctl command interacts with the SystemD service manager to manage the services. Contrary to service command, it manages the services by interacting with the SystemD process instead of running the init script.
To start, stop, and restart the Nginx process, we can run the respective commands with systemctl:
$ sudo systemctl start nginx
$ sudo systemctl stop nginx
$ sudo systemctl restart nginx
Additionally, we can check the status of the Nginx service using the status command:
$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-04-23 10:49:44 CEST; 16min ago
Docs: man:nginx(8)
Main PID: 6176 (nginx)
Tasks: 2 (limit: 4632)
Memory: 2.9M
CGroup: /system.slice/nginx.service
├─6176 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─6177 nginx: worker process
Apr 23 10:49:44 vagrant-VirtualBox systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 23 10:49:44 vagrant-VirtualBox systemd[1]: Started A high performance web server and a reverse proxy server.
We could also enable the Nginx service so that it is automatically started on system startup. To do that, we use the systemctl enable command:
$ sudo systemctl enable nginx
On the other hand, to remove it from the startup process list, we use the disable command:
$ sudo systemctl disable nginx
5. Summary
In this tutorial, we’ve explored the concept of the initialization process in Linux. Then, we’ve looked at the two different initialization systems that are prevalent in the current Linux landscape. Furthermore, we’ve also noted how the two different commands are for the different initialization systems. Finally, we’ve demonstrated both commands’ usage with the Nginx service.