In Linux servers, a “service” can be understood as a background program that runs continuously, similar to apps like “WeChat” or “Alipay” on mobile phones, always ready to respond to requests (e.g., a web service responding to user visits, a database service storing data). Managing the startup, stopping, and status checks of these services is a fundamental skill for Linux system maintenance. This article will guide you through the core methods of Linux service management in simple language.

一、Why Manage Services?

Imagine if the web service (e.g., Nginx or Apache) on your website server suddenly “crashes”—users won’t be able to access the site. In such cases, you need to restart the service. If server resources are tight, you might temporarily stop unused services (e.g., FTP) to save memory. Thus, mastering service startup, stopping, and status management is essential for operations and development.

二、Core Tools for Linux Service Management

Modern Linux systems (e.g., CentOS 7+, Ubuntu 16.04+) primarily use systemd as the service management tool, with the core command being systemctl. This tool can handle nearly all service-related operations, such as starting, stopping, restarting, and configuring auto-start on boot.

三、Basic Commands for Starting and Stopping Services

1. Start a Service

Format: sudo systemctl start <service-name>
Purpose: Immediately start the specified service (requires administrative privileges, executed with sudo or root).

Examples:
- Start Nginx (web server): sudo systemctl start nginx
- Start MySQL (database): sudo systemctl start mysql (or mariadb, depending on the installed version)
- Start Apache (web server): sudo systemctl start httpd

Note: The service name must be accurate. Common service names include:
Nginx→nginx, MySQL→mysql/mariadb, Apache→httpd, FTP→vsftpd, Firewall→firewalld (CentOS) or ufw (Ubuntu).

2. Stop a Service

Format: sudo systemctl stop <service-name>
Purpose: Immediately stop the specified service (if it is running).

Examples:
- Stop Nginx: sudo systemctl stop nginx
- Stop MySQL: sudo systemctl stop mysql

Note: systemctl stop forcefully terminates the service, which may cause data loss (e.g., unsaved files). For services requiring “graceful shutdown” (e.g., databases), try restart or reload first (covered later).

3. Restart a Service

Format: sudo systemctl restart <service-name>
Purpose: Stops the service first, then restarts it. Useful after modifying configuration files to apply changes (e.g., after editing Nginx config, restart to activate new settings).

Examples:
- Restart Nginx: sudo systemctl restart nginx

4. Check Service Status

Format: sudo systemctl status <service-name>
Purpose: View the current status of the service (running status, auto-start on boot, error logs, etc.).

Examples:
- Check Nginx status: sudo systemctl status nginx
- active (running) indicates the service is operational.
- inactive (dead) indicates the service is stopped.
- Red error messages may indicate issues (e.g., syntax errors in configuration files), which can be debugged via logs.

四、Auto-Start on Boot Configuration

Sometimes, you want services to start automatically after a server reboot (e.g., web servers, databases). This requires setting “auto-start on boot”.

1. Enable Auto-Start on Boot

Format: sudo systemctl enable <service-name>
Purpose: Add the service to the system startup list, so it runs automatically on the next boot.

Examples:
- Enable Nginx to start on boot: sudo systemctl enable nginx

2. Disable Auto-Start on Boot

Format: sudo systemctl disable <service-name>
Purpose: Remove the service from the system startup list, so it no longer starts automatically on boot.

Examples:
- Disable FTP service from starting on boot: sudo systemctl disable vsftpd

3. View Auto-Start Status of All Services

  • List all services: sudo systemctl list-unit-files --type=service
  • ENABLED indicates auto-start on boot; DISABLED indicates no auto-start.

五、Other Practical Operations

1. Reload Service Configuration (No Restart Needed)

Format: sudo systemctl reload <service-name>
Purpose: Send a “reload configuration” signal to the service (supported by services like Nginx and Apache), allowing changes to take effect without restarting the service.

Examples:
- After modifying Nginx configuration, run sudo systemctl reload nginx to apply changes.

2. View Service Logs

Format: sudo journalctl -u <service-name>
Purpose: View the service’s runtime logs (e.g., error reasons, startup time).

Examples:
- Check Nginx logs: sudo journalctl -u nginx
- Press Ctrl+C to exit log viewing.

六、Common Issues and Precautions

  1. Insufficient Permissions: systemctl commands require administrative privileges. Use sudo for regular users (e.g., sudo systemctl start nginx).
  2. Incorrect Service Name: Typos like ngix instead of nginx will trigger “unknown service”. Verify the service name with systemctl list-units --type=service.
  3. Service Not Installed: If systemctl start reports “service does not exist”, install the service first (e.g., sudo yum install nginx or sudo apt install nginx).
  4. Graceful Shutdown vs. Forced Stop:
    - stop forcefully terminates the service (risk of data loss).
    - restart stops and restarts (safer).
    - reload only reloads configuration (safest).

七、Summary

The core of Linux service management is the systemctl command. Master these operations to meet basic needs:
- Start: sudo systemctl start <service-name>
- Stop: sudo systemctl stop <service-name>
- Restart: sudo systemctl restart <service-name>
- Auto-start on boot: sudo systemctl enable <service-name> (disable: disable)
- Status check: sudo systemctl status <service-name>

Remember: Use the correct service name, confirm the service is installed before operations, and use sudo for privileged actions. Practice managing common services (e.g., Nginx, MySQL) to quickly become proficient!

Xiaoye