commands.page Website Logo

  • Home
  • Categories
  • Search

How to Stop a Service on the Terminal in Ubuntu

This is an article about how to stop services running on your Ubuntu operating system directly from the terminal. In this article, you will find detailed instructions and explanations on different methods to control services such as stopping them when they are not needed anymore, checking their status, and restarting or reloading configurations without interrupting service availability.

In this guide, we’ll walk through various commands and scripts that can be executed in the Terminal (also known as a command-line interface) using either Ubuntu’s Systemd system manager or other older systems management tools such as Upstart and SysVinit. We will also cover some troubleshooting tips to ensure your services are stopped properly.

Introduction

Managing processes on Linux distributions like Ubuntu is essential for maintaining system stability, security, and performance. One of the most common tasks involved in this process is stopping services that may no longer be needed or those which need to be restarted to apply changes to their configurations. This article will focus primarily on how to stop a service using the terminal on an Ubuntu machine.

Ubuntu uses Systemd as its default init system, replacing older systems like Upstart and SysVinit, starting from version 15.04 (Vivid Vervet). However, for completeness sake, we’ll briefly mention methods applicable to both Systemd-based systems and those still using Upstart or SysVinit.

Before proceeding, it’s important to ensure you have sudo privileges on your Ubuntu system to execute administrative commands like stopping services. For any of the examples in this guide, you will need to prefix the command with sudo (unless specified otherwise).

Why Stop a Service?

There are several reasons why you might want to stop a service:

  • Maintenance: To perform maintenance tasks such as upgrading or configuring software.
  • Security: Temporarily stopping services can improve system security by reducing the attack surface.
  • Resource Management: Stopping unnecessary services reduces resource usage, which is critical for maintaining high performance on servers with limited resources.
  • Testing and Debugging: It’s often necessary to stop a service temporarily when testing changes or diagnosing issues.

Using Systemctl Command (Systemd)

Basic Syntax

To interact with the Systemd system manager using the systemctl command, you need to know some basic syntax:

sudo systemctl [COMMAND] [SERVICE_NAME]

Where [COMMAND] can be one of several options, and [SERVICE_NAME] is the name of the service that you want to manage.

Common Commands

  • Start a Service: systemctl start SERVICE_NAME
  • Stop a Service: systemctl stop SERVICE_NAME
  • Restart a Service: systemctl restart SERVICE_NAME
  • Reload Configuration Files Without Restarting the Service: systemctl reload SERVICE_NAME
  • Check Status of a Service: systemctl status SERVICE_NAME

Example Usage

To stop Apache web server service (which has the name ‘apache2’ on Ubuntu), you would enter:

sudo systemctl stop apache2

This command will halt all instances of the Apache daemon and prevent them from starting until explicitly restarted.

Checking Status

Before attempting to restart or reload a configuration, it’s important to check if the service is running correctly. Use the status command to do so:

sudo systemctl status apache2

If you see an output indicating that the Apache service has been stopped successfully (e.g., “inactive (dead)” under Active state), then you know your previous stop operation was successful.

Using Upstart or SysVinit

Ubuntu systems before 15.04 used either Upstart or SysVinit as their init system, and services were managed through service commands in the terminal:

Basic Syntax for SysVinit

For managing services on older Ubuntu versions using SysVinit, use:

sudo service SERVICE_NAME [COMMAND]

Where [COMMAND] is one of several options (start, stop, restart).

Example Usage with Upstart or SysVinit

To stop the SSH daemon (ssh) via SysVinit command, you would run:

sudo service ssh stop

And to check its status:

sudo service ssh status

The output will typically indicate whether the service is running (‘active’) or stopped.

Using Service Script with Upstart

For services managed by Upstart (Ubuntu 12.04 LTS and earlier), you can similarly use:

sudo stop SERVICE_NAME

And to start it back up:

sudo start SERVICE_NAME

Troubleshooting Common Issues

Dependency Errors

Sometimes stopping a service might fail due to dependency issues. For instance, another service may rely on the one you’re trying to stop.

To address this issue, check dependencies with systemctl list-dependencies or use --ignore-dependencies flag when stopping:

sudo systemctl stop SERVICE_NAME --ignore-dependencies

Be cautious as ignoring dependencies can lead to unexpected behavior in your system.

Checking Logs

If you encounter problems while stopping a service, checking logs might provide clues. For Systemd-managed services:

journalctl -xeu SERVICE_NAME

And for SysVinit/Upstart managed services, use grep with the appropriate log file location (usually found in /var/log) to pinpoint errors.

Conclusion

Stopping a service on an Ubuntu system is straightforward once you understand how Systemd or older systems management tools work. Whether you’re using systemctl, service, or similar commands, following this guide should help you manage your services efficiently without risking system stability.

Remember that stopping critical services could affect the availability of important applications and network resources. Always ensure you have a backup plan or understand the implications before proceeding with any service management tasks.

Read this article to find out about best practices for managing Linux services on Ubuntu, ensuring your systems remain stable and secure while keeping resource usage optimal.

Last Modified: 22/05/2019 - 20:40:40