Linux service command
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
# Or use the equivalent systemctl command
systemctl start apache2
Stop the MySQL service:
Example
# Recommended for modern systems
systemctl stop mysql
Restart the Nginx service:
Example
# After modifying configuration files, a restart is usually required for changes to take effect
Check the SSH service status:
Example
# 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
# Use when only the configuration file has been modified and a full restart is not needed
List the status of all services:
Example
# [+] 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
chkconfig --list | grep httpd
# Modern systems use
systemctl is-enabled apache2
Practical application examples
Example 1: Managing a Web server
Example
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
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 systems
systemctlto gain more features
Common problem solving
Problem 1: Service fails to start
Example
# Output: Job for mysql.service failed because the control process exited with error code.
Solution:
- Check detailed error information:
journalctl -xe
- Check log files:
tail -n 50 /var/log/mysql/error.log
- Common causes: port conflict, permission issues, configuration file errors
Problem 2: Service command does not exist
service: command not found
Solution:
- Confirm whether it is in a minimal installation environment
- Install necessary packages:
# Debian/Ubuntu sudo apt install sysvinit-utils # RHEL/CentOS sudo yum install initscripts
Best practice recommendations
- Use the full path: It is recommended to use in production environments
/usr/sbin/servicerather than directly usingservice - Combine with systemctl: Prefer to use on modern systems
systemctlcommand - Add sudo: Service management usually requires root privileges
- Log checking: When a service is abnormal, first check the relevant logs
- 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 Command Reference