Skip to content

System management

Guide to System Monitoring and Management Commands in Linux and macOS

This guide provides an overview of key utilities for managing system services and monitoring logs in Linux and macOS. We'll cover systemctl, journalctl, service, and brew, including practical examples of commonly used commands. The guide is divided into two sections based on the operating system: Linux and macOS.


Linux System Monitoring and Management

1. systemctl

Description:
systemctl is the primary tool for managing system services in Linux distributions that use systemd as their init system. It controls the state of services, enables/disables them at startup, and checks their status.

Basic Usage:

  • Start a Service:

bash sudo systemctl start <service_name>

  • Stop a Service:

bash sudo systemctl stop <service_name>

  • Restart a Service:

bash sudo systemctl restart <service_name>

  • Enable a Service at Boot:

bash sudo systemctl enable <service_name>

  • Disable a Service at Boot:

bash sudo systemctl disable <service_name>

  • Check the Status of a Service:

bash sudo systemctl status <service_name>

  • List All Services:

bash systemctl list-units --type=service

Example:

sudo systemctl status nginx

This command checks the status of the nginx service, showing whether it's active, its process ID, and any recent logs.


2. journalctl

Description:
journalctl is a command used to view logs collected by the systemd journal. It provides access to logs generated by systemd services and the kernel.

Basic Usage:

  • View Recent Logs:

bash sudo journalctl -n 50

  • -n 50: Shows the last 50 log entries.

  • View Logs for a Specific Service:

bash sudo journalctl -u <service_name>

  • Follow Logs in Real-Time:

bash sudo journalctl -f

  • View Logs from the Last Boot:

bash sudo journalctl -b

Advanced Example:

sudo journalctl -xn 50 -u chef-client --no-pager
  • -x: Adds extra information where available.
  • -n 50: Shows the last 50 log entries.
  • -u chef-client: Filters logs for the chef-client service.
  • --no-pager: Outputs the log without using a pager, so it displays directly in the terminal.

This command is particularly useful for troubleshooting issues with specific services.


3. service

Description:
service is a command used to manage services on systems that may not use systemd (though it can still be found on systemd systems). It provides a way to start, stop, restart, and check the status of services.

Basic Usage:

  • Start a Service:

bash sudo service <service_name> start

  • Stop a Service:

bash sudo service <service_name> stop

  • Restart a Service:

bash sudo service <service_name> restart

  • Check the Status of a Service:

bash sudo service <service_name> status

Example:

sudo service apache2 restart

This command restarts the apache2 service.


macOS System Monitoring and Management

1. brew

Description:
brew is the package manager for macOS, also known as Homebrew. It allows you to install, update, and manage software packages and services on macOS.

Basic Usage:

  • Install a Package:

bash brew install <package_name>

  • Uninstall a Package:

bash brew uninstall <package_name>

  • List Installed Packages:

bash brew list

Managing Services with Homebrew:

Homebrew can also manage services (like databases, web servers, etc.) using the brew services command.

  • List All Services:

bash brew services list

  • Start a Service:

bash brew services start <service_name>

  • Stop a Service:

bash sudo brew services stop <service_name>

  • Restart a Service:

bash brew services restart <service_name>

Example:

brew services list
sudo brew services stop nginx
  • brew services list: Lists all services managed by Homebrew, showing their status.
  • sudo brew services stop nginx: Stops the nginx service.

Summary

This guide covers essential commands for managing and monitoring system services in Linux and macOS environments.

Linux:

  • systemctl and service are the primary tools for managing services, while journalctl is used for log monitoring.

macOS:

  • brew is the package manager, and brew services provides a convenient way to manage background services.