The Linux service command lets you run System V init scripts. Although many dis­tri­bu­tions for Linux now use other init systems, Linux service will still work for most cases.

What is the Linux service command?

Older Linux dis­tri­bu­tions in par­tic­u­lar still use the init system V designed to start and stop services properly. Services are programs that run auto­mat­ic­ally in the back­ground when you boot your computer. This usually concerns system services re­spons­ible for system startup and the hardware, and post-installed server services. These services usually work without com­mu­nic­a­tion with users and don’t have a graphical user interface. To execute a System V init script in­de­pend­ently the Linux service command is used.

How does Linux service work?

To run the service command, you’ll need root priv­ileges or should prepend the Linux sudo command. When you have the necessary priv­ileges, you can use the command to start, stop, reboot, or get the status from in­di­vidu­al scripts, among other things. The System V init scripts are located in the folder /etc/init.d/SCRIPT on Linux. Many dis­tri­bu­tions like Debian have switched to systemd. Linux service is still com­pat­ible with these. However, a better al­tern­at­ive for them is the Linux systemctl command. Basically, before you terminate or start a process, ensure it won’t neg­at­ively affect your operating system.

What is the syntax of the service command?

The basic syntax of Linux service is:

$ service [Script] [Action] [Option]
bash

If you don’t specify the ‘[Action]’ parameter, you’ll see all allowed actions in the command line.

What options and actions does Linux service have?

Strictly speaking, Linux service only has the default options ‘-h’ or ‘–help’, ‘-v’ or ‘–version’, and ‘-status-all’. However, you can specify the command with various actions. These are the most important ones:

  • start: Starts the script.
  • stop: Stops the script.
  • status: Shows the current status of the script.
  • restart: Ensures that the script is restarted.

Examples of using the Linux service command

To help you un­der­stand how the service command is used in practice, we’ll conclude with some examples of its main use.

$ sudo service sshd start
bash

This starts the sshd script.

$ sudo service sshd stop
bash

This forces the sshd script to stop.

$ sudo service sshd restart
bash

This command restarts the cor­res­pond­ing script.

$ sudo service sshd status
bash

This command is used to query the status of the script.

Go to Main Menu