Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Packages and Services

Linux service command

Reference for the Linux service command: start, stop, restart and check services through init scripts, how it maps to systemctl, and writing a simple init script.

service is a small wrapper that runs the init script of a service: service nginx restart executes /etc/init.d/nginx restart in a clean environment. It comes from the SysV init era, when each service was a shell script in /etc/init.d/ that understood the actions start, stop, restart and status.

On systemd systems the command still exists for compatibility: it forwards to systemctl, so the old muscle memory keeps working. You will meet it in older documentation, in containers and minimal images without systemd, and in the many Docker tutorials that call service ssh start.

Terminal
sudo service ssh status
sudo service nginx restart
service --status-all

Try it

The sandbox has no services to control, so both examples work with a sample init script and plain shell.

Bash A tiny init script (sample)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Which service tool is available?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
service SCRIPT COMMAND [OPTIONS]
service --status-all
service --version
Part Description
SCRIPT Name of a script in /etc/init.d/ (or a systemd unit when systemd is used).
start, stop, restart Start, stop, or stop and start the service.
reload Re-read the configuration without stopping, if the script supports it.
status Report whether the service runs.
force-reload, condrestart Restart-style actions; support depends on the script.
--status-all List all init scripts: [ + ] running, [ - ] stopped, [ ? ] unknown.

The set of actions is chosen by the script; there is no fixed list. A good script prints a usage line for unknown actions.

Returns: the exit status of the init script. LSB convention: 0 success, 1 generic failure, 3 for "not running" on status, 4 for "unknown".

Usage notes

  • Equivalent commands on systemd: service nginx start is systemctl start nginx; chkconfig and update-rc.d map to systemctl enable/disable. Prefer systemctl on modern systems.
  • Argument order is reversed: service NAME ACTION versus systemctl ACTION NAME.
  • Clean environment: service runs the script with a minimal PATH and without your variables, which can explain why a script behaves differently by hand.
  • Root is required for start, stop and restart; status often works for everyone.
  • In containers service nginx start only launches the daemon once and leaves it in the background; a container's main process should instead run in the foreground, otherwise the container exits.
  • Alpine uses OpenRC; its tool is rc-service and its init scripts live in /etc/init.d/ with a different format.
  • Writing init scripts: use #!/bin/sh, a case on $1, write a PID file, return LSB exit codes, and avoid Bash-only syntax. New services should be written as systemd units instead.
  • Unknown service produces "unrecognized service" (exit 1) or, on systemd, "Unit not found".