How To Manage Services In Linux

In this post I will be showing you how to manage services in Linux. This includes listing all available services and their respective status, starting, stopping, checking status, and restarting services. This is something that everyone who runs a Linux server or desktop needs to know, and while the title may sound intimidating it’s really quite simple.

List All Available Services And Respective Status

The following commands can be used to check the status of every service running on your Linux server or desktop. This will work for both Debian/Ubuntu based distributions as-well as RHEL/CentOS/Fedora based distributions.

sudo service --status-all # This will list all services
sudo service --status-all | grep <service name> # This will look for a specific service

Check Status Of Specific Service

If you need more information on the status of a specific service, you can use the following command. Again this will work for both Debian/Ubuntu based and RHEL/CentOS/Fedora based distributions. I have also included an example below as-well so you can see what kind of information the status argument provides.

sudo service <service name> status

# The below command will work on Ubuntu/Debian but not on RHEL 7/CentOS 7
sudo /etc/init.d/<service name> status
~# service apache2 status
● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
   Active: active (running) since Fri 2017-09-08 04:35:27 UTC; 1 day 18h ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1585 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCE
    Tasks: 11
   Memory: 183.5M
      CPU: 8min 36.160s
   CGroup: /system.slice/apache2.service
           ├─ 1724 /usr/sbin/apache2 -k start
           ├─15654 /usr/sbin/apache2 -k start
           ├─15734 /usr/sbin/apache2 -k start
           ├─15746 /usr/sbin/apache2 -k start
           ├─15771 /usr/sbin/apache2 -k start
           ├─15841 /usr/sbin/apache2 -k start
           ├─15882 /usr/sbin/apache2 -k start
           ├─15883 /usr/sbin/apache2 -k start
           ├─15918 /usr/sbin/apache2 -k start
           ├─16330 /usr/sbin/apache2 -k start
           └─16904 /usr/sbin/apache2 -k start

Start/Stop/Restart/Reload A Specific Service

You can use the same command as the last few examples to start/stop/restart/reload services. Please note that when applying changes that require a service restart or reload, a good idea is to make a backup of whatever configuration file you change. This way you can revert back to the original configuration should the changes make the service unable to start (it happens!). I will include an example of a backup command as-well below.

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak # Backup configuration file
sudo service <service name> start # Start service
sudo service <service name> stop # Stop service
sudo service <service name> restart # Restart service
sudo service <service name> reload # Reload service

# The below commands will work on Ubuntu/Debian but not on RHEL 7/CentOS 7
sudo /etc/init.d/<service name> start # Start service
sudo /etc/init.d/<service name> stop # Stop service
sudo /etc/init.d/<service name> restart # Restart service
sudo /etc/init.d/<service name> reload # Reload service

Enable And Disable Services During Boot/Startup

Last but not least, we need to be able to control whether or not a service starts when your desktop/server does. This will vary based on which distribution you are using, I have included both the chkconfig method and update-rc.d methods.

sudo update-rc.d <service name> enable # Enable service on Ubuntu/Debian and RHEL 6/CentOS 6 (and below)
sudo update-rc.d <service name> disable # Disable service on Ubuntu/Debian and RHEL 6/CentOS 6 (and below)
sudo chkconfig <service name> on # Enable service on RHEL 7/CentOS 7
sudo chkconfig <service name> off # Disable service on RHEL 7/CentOS 7

I hope this post on managing services was helpful. Please don’t forget to like/comment/share, thanks!

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *