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

  1. systemctl command
  2. service command
  3. 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.

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

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.

3. Start service using systemctl

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

ubuntu-systemctl-command-start-stop-restart-status

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.

In the service command, we have to first specify the service name and then the command to execute.

Stop a Service:

Start a Service:

Restart a Service:

Checking Service Status:

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:

Start a Service:

Restart a Service:

Checking Service Status:

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.

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.

By admin

Leave a Reply

%d bloggers like this: