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.
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.
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.
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
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 startissystemctl start nginx;chkconfigandupdate-rc.dmap tosystemctl enable/disable. Prefersystemctlon modern systems. - Argument order is reversed:
service NAME ACTIONversussystemctl ACTION NAME. - Clean environment:
serviceruns the script with a minimalPATHand without your variables, which can explain why a script behaves differently by hand. - Root is required for start, stop and restart;
statusoften works for everyone. - In containers
service nginx startonly 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-serviceand its init scripts live in/etc/init.d/with a different format. - Writing init scripts: use
#!/bin/sh, acaseon$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".
Related
systemctl,journalctl,killall- Lessons: Packages and services, Boot and kernel