Linux service Command
What is the service command
service The service command is a command-line tool in Linux systems 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 running in the background, usually providing key system functions (such as networking, logging, databases, etc.). Understanding the service command is essential for Linux system administration.
Basic syntax of the service command
service [服务名] [操作指令]
Common operation commands
| Instruction | 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 a service |
| --status-all | List the status of all services |
Common Parameters in Detail
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
Modern systems recommend using
systemctl stop mysql
Restart the Nginx service:
Example
service nginx restart
After modifying a configuration file, a restart is usually required 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 all service statuses:
Example
service
--status-all
[+] indicates a running service
[-] indicates a stopped service
[?] indicates a service whose status is unknown
Check whether a service is configured to start on 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. Reload after modifying the configuration file
sudo
nano
/
etc
/
apache2
/
apache2.conf
sudo
service apache2 reload
3. Check the 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 the service status
service mysql status
Differences between old and new systems
As Linux systems have evolved, service management methods have changed:
| Feature | Traditional systems (SysVinit) | Modern systems (systemd) |
|---|---|---|
| Service management commands | service | systemctl |
| Config file locations | /etc/init.d/ | /lib/systemd/ |
| Log management | Scattered log files | journalctl |
| Parallel startup | Not supported | Supported |
Compatibility notes:
- In modern systems,
servicethe command is usually asystemctlcompatibility wrapper for - New systems are recommended to use
systemctlto get more features
Common Problem Solutions
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:
- View detailed error messages:
journalctl -xe
- Check the log files:
tail -n 50 /var/log/mysql/error.log
- Common causes: port conflicts, permission issues, configuration file errors
Problem 2: The service command does not exist
service: command not found
Solution:
- Check whether you are in a minimal installation environment
- Install the necessary software packages:
Debian/Ubuntu
sudo apt install sysvinit-utils
RHEL/CentOS
sudo yum install initscripts
Best Practice Recommendations
- Use the full path: In production environments, it is recommended to use
/usr/sbin/servicerather than directly usingservice - Combined with systemctl: Modern systems prefer to use
systemctlCommand - Add sudo: Service management usually requires root privileges
- Log checking: When a service is abnormal, first check the relevant logs
- Boot startup management:
Enable boot startup
sudo systemctl enable nginx
Disable boot startup
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 runlevels | chkconfig --list |
| update-rc.d | Debian-based runlevel management (Debian) | update-rc.d apache2 defaults |
Through this article, you should have mastered the core usage of the service command in Linux systems. Remember, although the service command is common in older systems, in modern Linux distributions that use systemd,systemctl a more recommended service management tool. It is advisable to choose the appropriate command based on your system environment and develop the good habit of checking service logs.
Linux Command Reference