OS: Linux Process and System Monitoring

In Linux (and all operating systems), a process is simply a program in execution. When you run a command or start an application, the system loads the program’s code into memory and manages it as a process.

What is a Process?

Key Characteristics of a Process

  • PID (Process ID) → A unique number assigned to every process. Example: 1234.
  • Parent/Child → Processes are created by other processes. The original one is init (PID 1, usually systemd).
  • Resources → Each process uses CPU, memory, and I/O.
  • State → A process can be running, sleeping (waiting), stopped, or terminated.

Examples of Processes

  • When you open Firefox → a process is created (actually multiple).
  • When you run ls in the terminal → it becomes a short-lived process.
  • Background services (like networking, logging) → run as processes.

Process Management

In Linux, processes run constantly in the background or foreground. To keep the system stable and responsive, you need tools to view, monitor, and control these processes.

Viewing Processes

  • ps → snapshot of active processes.
  • bash
    ps aux

  • a → processes from all users
  • u → show user info
  • x → include background/daemon processes
  • Output:

    bash
    USER   PID  %CPU  %MEM COMMAND
    amr   1342   0.2   1.1  bash
    amr   1456   3.4   5.6  firefox
    root   789   0.0   0.3  sshd

  • top → live view of running processes.
  • bash
    top

    The top command gives a live updating view of processes.

  • Shows CPU %, memory usage, running time, and PID.
  • Useful for finding processes consuming too many resources.
  • Common keys inside top:
  • q → quit
  • k → kill a process (enter PID)
  • P → sort by CPU usage
  • M → sort by memory usage
  • htop (if installed) → interactive process viewer.
  • bash
    htop

  • process = a running program.
  • Identified by PID.
  • Can be managed (started, stopped, killed, put in background/foreground).
  • Processes are the building blocks of the Linux system — from desktop apps to hidden system services.

Terminate Processes

The kill command sends a signal to a process, usually to stop it.

  • Kill by PID:
  • bash
    kill 1234

  • Force kill (if normal kill doesn’t work):
  • bash
    kill -0 1234

  • Kill by name (all processes with that name):
  • bash
    pkill firefox


Services & Daemons (systemctl, service)

In Linux, some processes run in the background without direct user interaction. These are known as daemons. They often start automatically at boot and keep running to provide essential services like networking, logging, and scheduling.

What is a Daemon?

  • daemon is a background process that waits for requests or performs tasks continuously.
  • Examples:
  • sshd → allows remote login via SSH.
  • cron → schedules tasks.
  • cupsd → manages printers.
  • Daemons usually have names ending with d.

What is a Service?

  • service is a managed daemon (or group of processes) that the operating system controls.
  • Services are typically started/stopped/enabled through a service manager.
  • On modern Linux, this is usually systemd, controlled via systemctl.

Managing Services

With `systemctl` (systemd)

  • Start a service:
  • bash
    sudo systemctl start ssh

  • Stop a service:
  • bash
    sudo systemctl stop ssh

  • Restart a service:
  • bash
    sudo systemctl restart ssh

  • Check service status:
  • bash
    sudo systemctl status ssh

  • Enable a service to start at boot:
  • bash
    sudo systemctl enable ssh

  • Disable service at boot:
  • bash
    sudo systemctl disable ssh

Legacy Command: `service`

Older Linux systems (before systemd) use the service command. Some still support it for compatibility.

  • Start service:
  • bash
    sudo service ssh start

  • Stop service:
  • bash
    sudo service ssh stop

  • Check status:
  • bash
    sudo service ssh status

  • Daemon → a background process (e.g., sshdcron).
  • Service → a daemon managed by the OS.
  • systemctl → modern tool to control services.
  • service → older command, sometimes still available.