How to Stop a Service in Linux: Simple Commands Explained
To stop a service in Linux, use the
systemctl stop service-name command on systems with systemd. On older systems, use service service-name stop. Replace service-name with the actual service you want to stop.Syntax
There are two common commands to stop services in Linux depending on your system's init system:
- systemctl stop service-name: Stops the service using systemd, the modern init system.
- service service-name stop: Stops the service on older systems using SysVinit or Upstart.
Replace service-name with the exact name of the service you want to stop.
bash
systemctl stop service-name service service-name stop
Example
This example shows how to stop the apache2 web server service using systemctl. It demonstrates the command and the expected output when successful.
bash
sudo systemctl stop apache2 sudo systemctl status apache2
Output
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2024-06-07 10:00:00 UTC; 2s ago
Main PID: 1234 (code=exited, status=0/SUCCESS)
Jun 07 10:00:00 hostname systemd[1]: Stopped The Apache HTTP Server.
Common Pitfalls
Common mistakes when stopping services include:
- Using the wrong command for your system's init system (e.g., using
serviceon a systemd system). - Not running the command with
sudoor as root, which is required to stop services. - Misspelling the service name, causing the command to fail silently or show an error.
- Expecting the service to stop immediately without checking its status.
bash
sudo service apache2 stop # Older systems sudo systemctl stop apache2 # Modern systems # Wrong: missing sudo systemctl stop apache2 # Wrong: misspelled service name sudo systemctl stop apche2
Quick Reference
| Command | Description |
|---|---|
| systemctl stop service-name | Stop a service on systemd-based systems |
| service service-name stop | Stop a service on SysVinit or Upstart systems |
| sudo systemctl status service-name | Check the status of a service |
| sudo systemctl start service-name | Start a service |
| sudo systemctl restart service-name | Restart a service |
Key Takeaways
Use
systemctl stop service-name on modern Linux systems with systemd.Use
service service-name stop on older Linux systems without systemd.Always run stop commands with
sudo or as root to have permission.Double-check the service name to avoid errors.
Verify the service status after stopping to confirm it stopped successfully.