Linux service command

Linux 命令大全Linux Command Reference


What is the service command?

serviceIt is a command-line tool in Linux for managing system services. It provides a standardized way to start, stop, restart, and check the status of system services.

A service is an application or process that runs in the background, usually providing critical system functions (such as networking, logging, database, etc.). Understanding the service command is crucial for Linux system administration.


service command basic syntax

service [服务名] [操作指令]

Common operation instructions

Command Description
start Start the specified service
stop Stop the specified service
restart Restart the specified service
reload Reload the configuration file (without restarting the service)
status View the running status of the service
--status-all List the status of all services

Detailed explanation of common parameters

1. Basic service management operations

Start the Apache service:

Example

service apache2 start
# Or use the equivalent systemctl command
systemctl start apache2

Stop the MySQL service:

Example

service mysql stop
# Recommended for modern systems
systemctl stop mysql

Restart the Nginx service:

Example

service nginx restart
# After modifying configuration files, a restart is usually required for changes to take effect

Check the SSH service status:

Example

service sshd status
# Example output:
# ● ssh.service - OpenBSD Secure Shell server
#    Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
#    Active: active (running) since Tue 2023-05-16 10:23:45 CST; 3h 25min ago

2. Advanced usage

Reload service configuration (without interrupting the service):

Example

service nginx reload
# Use when only the configuration file has been modified and a full restart is not needed

List the status of all services:

Example

service --status-all
# [+] indicates a running service
# [-] indicates a stopped service
# [?] indicates a service with unknown status

Check whether the service is configured to start at boot:

Example

# Old-style method
chkconfig --list | grep httpd
# Modern systems use
systemctl is-enabled apache2

Practical application examples

Example 1: Managing a Web server

Example

# 1. Start Apache
sudo service apache2 start

# 2. After modifying the configuration file, reload
sudo nano /etc/apache2/apache2.conf
sudo service apache2 reload

# 3. Check running status
service apache2 status

Example 2: Database service maintenance

Example

# 1. Stop MySQL for maintenance
sudo service mysql stop

# 2. Perform maintenance operations...

# 3. Restart MySQL
sudo service mysql start

# 4. Verify service status
service mysql status

Differences between old and new systems

As Linux systems have evolved, service management methods have changed:

Feature Traditional system (SysVinit) Modern system (systemd)
Service management command service systemctl
Configuration file location /etc/init.d/ /lib/systemd/
Log management Scattered log files journalctl
Parallel startup Not supported Supported

Compatibility notes:

  • In modern systems,servicethe command is usuallysystemctla compatibility wrapper
  • It is recommended to use on new systemssystemctlto gain more features

Common problem solving

Problem 1: Service fails to start

Example

sudo service mysql start
# Output: Job for mysql.service failed because the control process exited with error code.

Solution:

  1. Check detailed error information:
    journalctl -xe
  2. Check log files:
    tail -n 50 /var/log/mysql/error.log
  3. Common causes: port conflict, permission issues, configuration file errors

Problem 2: Service command does not exist

service: command not found

Solution:

  1. Confirm whether it is in a minimal installation environment
  2. Install necessary packages:
    # Debian/Ubuntu
    sudo apt install sysvinit-utils
       
    # RHEL/CentOS
    sudo yum install initscripts

Best practice recommendations

  1. Use the full path: It is recommended to use in production environments/usr/sbin/servicerather than directly usingservice
  2. Combine with systemctl: Prefer to use on modern systemssystemctlcommand
  3. Add sudo: Service management usually requires root privileges
  4. Log checking: When a service is abnormal, first check the relevant logs
  5. Managing auto-start at boot:
    # 启用开机启动
    sudo systemctl enable nginx
       
    # 禁用开机启动
    sudo systemctl disable nginx

Knowledge extension

Comparison of related commands

Command Purpose Example
service Compatibility service management service sshd restart
systemctl Modern service management (recommended) systemctl restart sshd
chkconfig Manage SysVinit run levels chkconfig --list
update-rc.d Debian-based run level management (Debian) update-rc.d apache2 defaults

Through this article, you should have mastered the core usage of the service command in Linux. Remember that although the service command is common in old systems, in modern Linux distributions using systemd,systemctlis the more recommended service management tool. It is recommended to choose the appropriate command based on your system environment, and develop a good habit of checking service logs.

Linux 命令大全Linux Command Reference

Other extensions