Systemctl to Restart/Start/Stop Services in Linux

Systemd is a system and service manager for Linux operating systems. It is designed to be backward compatible with SysV init scripts, and most modern Linux distributions had now adopted systemd.

Previous versions of Linux, which were distributed with SysV init or Upstart, used init scripts written in bash located in the /etc/rc.d/init.d/ directory. These init scripts have been replaced with service units.

To start, stop, restart, enable, or disable Linux service you will use the systemctl command instead of the old service command.

Syntax :

systemctl [command] [service_name]

Start a Service Using systemctl

To start a service run systemctl command followed by 'start' then space service name.

Syntax:

systemctl start service-name

Example - To start nginx service using systemctl, run:

$ sudo systemctl start nginx

Stop a service using systemctl

To stop a service run systemctl command followed by 'stop' then space service name.

Syntax:

systemctl stop service-name

For example to stop nginx service, use:

$ sudo systemctl stop nginx

Restart a service using systemctl

To restart a service use systemctl followed by 'restart' then space service name. You can also use the try-restart option that will restart the service only if it's already running. Also, you have the reload option that will reload the configuration files.

Syntax:

systemctl restart service-name

Examples:

$ sudo systemctl restart nginx
$ sudo systemctl try-restart nginx
$ sudo systemctl reload nginx

Check status of a service using systemctl

To check the status of a service use systemctl followed by 'status' then space service name.

Syntax:

systemctl status service-name

Example:

$ sudo systemctl status nginx

Enable/Disable service at boot time

You can use the systemctl enable/disable options to make a service run at boot time.

Syntax:

systemctl enable/disable service-name

For example, let's check how to enable and disable Nginx web server service on system startup:

$ sudo systemctl enable nginx
$ sudo systemctl disable nginx

Conclusion

To reboot your Linux computer type sudo systemctl reboot. I hope you enjoyed reading this article on knowing how to use systemctl command to restart, start and stop service in Linux.

2 Comments... add one

  1. Adrian, I found this post to be very, extremely helpful. My original search was to find an example of an httpd.service file (the only thing missing from your post). I don't know if I've got it right, but I put together an apache-httpd.service file after reading the man pages for systemd.service and systemctl - and countless web search results. What I came up with (seems to work) is the following:

    file: /etc/systemd/system/apache-httpd.service (symlink to /usr/lib/systemd/system/apache-httpd.service)

    ------ File Begins Here -----
    [Unit]
    Description=Apache HTTPD Web Server
    After=network.target

    [Service]
    Type=forking
    PIDFile=/var/run/httpd/httpd.pid
    ExecStart=/sbin/apachectl start
    ExecStop=/sbin/apachectl graceful-stop
    ExecReload=/sbin/apachectl graceful
    PrivateTmp=true
    LimitNOFILE=infinity

    [Install]
    WantedBy=multi-user.target

    ------ File Ends Here -----

    Maybe this will help others stumbling around in the dark on this new (very cool) way to manage services...

    -DM

    Reply

Leave a Comment