When an application runs in the background, it’s called a service. These are essential to run the system or our software applications. Some of the common services you will find on most of the servers are Apache, MySQL, NGINX, etc. When the system boots up, these services are configured to automatically startup.
I am using Ubuntu to host my websites. I also use MySQL to store all my websites data. Sometimes, I perform regular updates and it’s essential to restart these services. In this tutorial, we will learn various ways to start, stop, and restart services in Ubuntu.
Different Ways to Start, Stop, Restart Services on Ubuntu
- systemctl command
- service command
- init scripts
Using systemctl to start, stop, and restart Ubuntu services
This is the preferred way to manage Ubuntu services. If you are not sure of the service name, you can run systemctl --all
command to list all the services. But, there might be hundreds of services running, so it’s better to filter the list using the grep
command.
Let’s try to find out the service names of MySQL and Apache HTTP server.
1 2 3 4 5 |
# systemctl --all | grep -i mysql mysql.service loaded active running MySQL Community Server # systemctl --all | grep -i apache apache2.service loaded active running The Apache HTTP Server # |
So, the MySQL service name is “mysql.service” and Apache HTTP server service name is “apache2.service”.
Let’s learn how to manage these services using the systemctl command.
1. Stop service using systemctl
1 2 |
# systemctl stop mysql.service # |
The command doesn’t give any output if the execution is successful.
2. Checking Service Status using systemctl
We can check the service status using the below command.
1 2 3 4 5 |
# systemctl status mysql.service ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: inactive (dead) since Sat 2020-05-02 17:39:22 UTC; 9s ago Main PID: 26948 (code=exited, status=0/SUCCESS) |
3. Start service using systemctl
1 2 3 4 5 6 7 8 9 10 11 12 |
# systemctl start mysql.service # # systemctl status mysql.service ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-05-02 17:41:43 UTC; 3s ago Process: 30254 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid (code=exited, s Process: 30233 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 30256 (mysqld) Tasks: 27 (limit: 2318) CGroup: /system.slice/mysql.service └─30256 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid |
Notice that when we had stopped the service, the “Active” value was inactive (dead). After starting the service, it’s changed to active (running).
4. Restart service using systemctl
1 2 3 4 5 6 7 |
# systemctl restart apache2.service # systemctl status apache2.service ● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Sat 2020-05-02 17:55:09 UTC; 8s ago |
Manage Ubuntu Services using service Command
We can list all the services using service --status-all
command. If needed, use the grep command to filter out the service you are looking for.
1 2 3 |
# service --status-all | grep mysql [ + ] mysql # |
In the service command, we have to first specify the service name and then the command to execute.
Stop a Service:
1 |
# service mysql stop |
Start a Service:
1 |
# service mysql start |
Restart a Service:
1 |
# service mysql restart |
Checking Service Status:
1 2 3 4 5 6 7 8 9 10 |
# service mysql status ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-05-02 18:19:34 UTC; 39s ago Process: 31768 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid (code=exited, s Process: 31746 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 31770 (mysqld) Tasks: 27 (limit: 2318) CGroup: /system.slice/mysql.service └─31770 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid |
Ubuntu init scripts to Manage Services
The services init scripts are located in /etc/init.d/
directory. We can use these scripts to manage the services. However, it’s not recommended to use them anymore and it’s better to use the systemctl command.
Stop a Service:
1 2 3 |
# /etc/init.d/mysql stop [ ok ] Stopping mysql (via systemctl): mysql.service. # |
Start a Service:
1 2 3 |
# /etc/init.d/mysql start [ ok ] Starting mysql (via systemctl): mysql.service. # |
Restart a Service:
1 2 3 |
# /etc/init.d/mysql restart [ ok ] Restarting mysql (via systemctl): mysql.service. # |
Checking Service Status:
1 2 3 4 |
# /etc/init.d/mysql status ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-05-02 18:26:30 UTC; 17s ago |
Permission issues when starting/stopping services
If you are not logged in as root user, the above commands will ask you to provide root user password to execute. If the wrong password is entered, the authentication failed error will be thrown and command will not be executed.
If you are on the sudoers list, you can run these commands as a sudo user. If you are not on the sudoers list, an error message will be displayed that you are not on the sudoers list and the incident will be reported.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
test@localhost:~$ systemctl stop mysql ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === Authentication is required to stop 'mysql.service'. Authenticating as: root Password: polkit-agent-helper-1: pam_authenticate failed: Authentication failure ==== AUTHENTICATION FAILED === Failed to stop mysql.service: Access denied See system logs and 'systemctl status mysql.service' for details. test@localhost:~$ sudo systemctl stop mysql [sudo] password for test: test is not in the sudoers file. This incident will be reported. test@localhost:~$ |
So if you want to manage any service, make sure you have necessary privileges to execute these commands.
Conclusion
We learned various ways to manage services on Ubuntu. The systemctl command is the preferred approach to start/stop/restart services on Ubuntu. However, the init scripts print the command status, which can be useful in shell scripts to run them and immediately get the status of the command.