Service restart command in linux

How to Start, Stop & Restart Services in Ubuntu and Other Linux Distributions

Services are essential background processes that are usually run while booting up and shut down with the OS.

If you are a sysadmin, you’ll deal with the service regularly.

If you are a normal desktop user, you may come across the need to restart a service like setting up Barrier for sharing mouse and keyboard between computers. or when you are using ufw to setup firewall.

Today I will show you two different ways you can manage services. You’ll learn to start, stop and restart services in Ubuntu or any other Linux distribution.

Ubuntu and many other distributions these days use systemd instead of the good old init.

In systemd, you manage sevices with systemctl command.

In init, you manage service with service command.

You’ll notice that even though your Linux system uses systemd, it is still able to use the service command (intended to be used with init system). This is because service command is actually redirect to systemctl. It’s sort of backward compatibility introduced by systemd because sysadmins were habitual of using the service command.

I’ll show both systemctl and service command in this tutorial.

I am Ubuntu 18.04 here, but the process (no pun intended) is the same for other versions.

Method 1: Managing services in Linux with systemd

I am starting with systemd because of the obvious reason of its widespread adoption.

1. List all services

In order to manage the services, you first need to know what services are available on your system.

You can use the systemd command to list all the services on your Linux system:

systemctl list-unit-files --type service -all

systemctl list-unit-files

This command will output the state of all services. The value of a service’s state can be enabled, disabled, masked (inactive until mask is unset), static and generated.

Combine it with the grep command and you can display just the running services:

sudo systemctl | grep running

Display running services systemctl

Now that you know how to reference all different services, you can start actively managing them.

Читайте также:  Посмотреть подключенных пользователей linux ssh

Note: name> in the commands should be replaced by the name of the service you wish to manage (e.g. network-manager, ufw etc.).

2. Start a service

To start a service in Linux, you just need to use its name like this:

3. Stop a service

To stop a systemd service, you can use the stop option of systemctl command:

4. Restart a service

To restart a service in Linux with systemd, you can use:

5. Check the status of a service

You can confirm that you have successfully executed a certain action by printing the service status:

This will output information in the following manner:

systemctl status

That was systemd. Let’s switch to init now.

Method 2: Managing services in Linux with init

The commands in init are also as simple as system.

1. List all services

To list all the Linux services, use

service --status-all

The services preceded by [ – ] are disabled and those with [ + ] are enabled.

2. Start a service

To start a service in Ubuntu and other distributions, use this command:

3. Stop a service

Stopping a service is equally easy.

4. Restart a service

If you want to restart a service, the command is:

5. Check the status of a service

Furthermore, to check if your intended result was achieved, you can output the service status:

This will output information in the following manner:

service status

This will, most importantly, tell you if a certain service is active (running) or not.

Wrapping Up

Today I detailed two very simple methods of managing services on Ubuntu or any other Linux system. I hope this article was helpful to you.

Which method do you prefer? Let us know in the comment section below!

Источник

How to Start, Stop, and Restart Services in Linux

Linux provides fine-grained control over system services through systemd, using the systemctl command. Services can be turned on, turned off, restarted, reloaded, or even enabled or disabled at boot. If you are running Debian 7, CentOS 7, or Ubuntu 15.04 (or later), your system likely uses systemd.

This guide will show you how to use basic commands to start, stop, and restart services in Linux.

Tutorial on how to start, stop, and restart services on Linux.

  • Access to a user account with sudo or root privileges
  • Access to a terminal/command line
  • The systemctl tool, included in Linux

Basic Syntax of systemctl Command

The basic syntax for using the systemctl command is:

systemctl [command] [service_name]

Typically, you’ll need to run this as a superuser with each command starting with sudo .

How To Check If a Service is Running on Linux

To verify whether a service is active or not, run this command:

sudo systemctl status apache2

Replace apache2 with the desired service. In our case, we checked the status of Apache. The output shows that the service is active (running), as in the image below:

example that a service is active and running in Linux

How to Restart a Service

To stop and restart the service in Linux, use the command:

sudo systemctl restart SERVICE_NAME

After this point, your service should be up and running again. You can verify the state with the status command.

sudo systemctl restart apache2

systemctl command restarting service

How to Reload a Service

To force the service to reload its configuration files, type in the following command in the terminal:

sudo systemctl reload SERVICE_NAME

After reloading, the service is going to be up and running. Check its state with the status command to confirm.

In our example, we reloaded Apache using:

sudo systemctl reload apache2

reloading a service in linux

How to Start a Service

To start a service in Linux manually, type in the following in the terminal:

sudo systemctl start SERVICE_NAME

For instance, the command to start the Apache service is:

sudo systemctl start apache2

How to Stop a Service

To stop an active service in Linux, use the following command:

sudo systemctl stop SERVICE_NAME

If the service you want to stop is Apache, the command is:

sudo systemctl stop apache2

Check whether the service stopped running with the status command. The output should show the service is inactive (dead).

systemctl stop service example

How to Enable the Service at Boot

To configure a service to start when the system boots, use the command:

sudo systemctl enable SERVICE_NAME

To enable Apache upon booting the system, run the command:

sudo systemctl enable apache2

systemctl enable service at boot

How to Disable Service at Boot

You can prevent the service from starting at boot with the command:

sudo systemctl disable SERVICE_NAME
sudo systemctl disable apache2

Variations in Service Names

If you work within the same Linux environment, you will learn the names of the services you commonly use.

For example, if you are building a website, you will most likely use systemctl restart apache2 frequently, as you refresh configuration changes to your server.

However, when you move between different Linux variants, it is helpful to know that the same service may have different names in different distributions.

For example, in Ubuntu and other Debian based distributions, the Apache service is named apache2. In CentOS 7 and other RedHat distros, the Apache service is called httpd or httpd.service.

apache web service names of https and apache2

In most modern Linux operating systems, managing a service is quite simple using the commands presented here.

Make sure to know the name of the service for the operating system you are using, and the commands in this article should always work.

Источник

How to restart service in Linux

Different distributions have different ways of service management. Even within the same distro, different versions may have different service management aspects. Like RHEL 6 and RHEL7 has different commands to manage services.

Let’s see service related tasks in various flavors of Linux –

How to stop service in Linux

Service can be stopped with below commands (respective distro specified)

# service stop (RHEL6 & lower, Ubuntu, CentOS, Debian, Fedora) # systemctl stop .service (RHEL7) # stop (Ubuntu with upstart)

here is service name like telnet, NTP, NFS, etc. Note that upstart is pre-installed with Ubuntu 6.10 later, if not you can install using the APT package.

Newer versions are implementing systemctl now in place of service command. Even if you use service command in RHEL7 then it will call systemctl in turns.

# service sshd-keygen status Redirecting to /bin/systemctl status sshd-keygen.service ● sshd-keygen.service - OpenSSH Server Key Generation Loaded: loaded (/usr/lib/systemd/system/sshd-keygen.service; static; vendor preset: disabled) Active: inactive (dead) -----output clipped-----

In the above output, you can see it shows you which systemctl command its executing in place of service command. Also, note that it appends .service to service_name supplied to service command.

Old service commands like RHEL6 & lower, prints status of operation as OK (success) or FAILED (failure) for start, stop, restart operations. systemctl the command doesn’t print any output on the console.

How to start service in Linux

Starting service follows same above syntax.

# service start (RHEL6 & lower, Ubuntu, CentOS, Debian, Fedora) # systemctl start .service (RHEL7) # start (Ubuntu with upstart)

How to restart service in Linux

# service restart (RHEL6 & lower, Ubuntu, CentOS, Debian, Fedora) # systemctl restart .service (RHEL7) # restart (Ubuntu with upstart)

It stops service and then immediately starts it. So basically its a combined command of above two.

Mostly to reload edited new configuration we seek restart of service. But this can be done without restarting it provided service supports reload config. This can be done by using reload option instead of restart .

How to check the status of service in Linux

Checking the status of service makes you aware of if service is currently running or not. Different distros give different details about service in the output of status. Below are a few examples for your reference.

Service status information in Ubuntu :

# service cron status ● cron.service - Regular background program processing daemon Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2017-03-10 17:53:23 UTC; 2s ago Docs: man:cron(8) Main PID: 3506 (cron) Tasks: 1 Memory: 280.0K CPU: 1ms CGroup: /system.slice/cron.service └─3506 /usr/sbin/cron -f Mar 10 17:53:23 ip-172-31-19-90 systemd[1]: Started Regular background program processing daemon. Mar 10 17:53:23 ip-172-31-19-90 cron[3506]: (CRON) INFO (pidfile fd = 3) Mar 10 17:53:23 ip-172-31-19-90 cron[3506]: (CRON) INFO (Skipping @reboot jobs -- not system startup)

It has details about the service state, its man page, PID, CPU & MEM utilization, and recent happenings from the log.

Service status information in RHEL6:

# service crond status crond (pid 1474) is running.

It only shows you PID and state of service.

Service status information in RHEL7:

# systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2017-03-10 13:04:58 EST; 1min 2s ago Main PID: 499 (crond) CGroup: /system.slice/crond.service └─499 /usr/sbin/crond -n Mar 10 13:04:58 ip-172-31-24-59.ap-south-1.compute.internal systemd[1]: Started Command Scheduler. Mar 10 13:04:58 ip-172-31-24-59.ap-south-1.compute.internal systemd[1]: Starting Command Scheduler. Mar 10 13:04:58 ip-172-31-24-59.ap-south-1.compute.internal crond[499]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 85% if used.) Mar 10 13:04:59 ip-172-31-24-59.ap-south-1.compute.internal crond[499]: (CRON) INFO (running with inotify support)

It prints all details as Ubuntu but doesn’t show CPU and memory utilization, manpage.

List all services on the system

If you want to see all services running on the system and their statuses then you can use below command :

# service --status-all (RHEL6 & lower, Ubuntu, CentOS, Debian, Fedora) # systemctl list-units --type service --all (RHEL7)

It will present you list of all services and their status with few other details.

Источник

Оцените статью
Adblock
detector