karafka/karafka

View on GitHub
bin/record_rss

Summary

Maintainability
Test Coverage
#!/bin/bash

# This script monitors and records the Resident Set Size (RSS) of a process given its PID.
# The RSS is logged every second to the specified output file until the process terminates.
#
# Usage:
# ./script_name.sh <PID> <OUTPUT_FILE>
#
# Arguments:
# <PID>         - Process ID of the process you want to monitor.
# <OUTPUT_FILE> - Name of the file where RSS values will be logged.
#
# The script first checks if the correct number of arguments are provided.
# It then verifies if the given PID exists. If it does, it starts recording the RSS.
# For every iteration, the script fetches the current RSS of the process using the 'ps' command,
# then appends the RSS value along with a timestamp to the output file.
# This recording is done every second.
# The loop stops if the process with the given PID terminates.
# An informative message is printed out when recording starts and when it stops.

# Check if the correct number of arguments are passed
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <PID> <OUTPUT_FILE>"
    exit 1
fi

PID=$1
OUTPUT_FILE=$2

# Check if the given PID exists
if ! kill -0 $PID 2>/dev/null; then
    echo "Error: PID $PID does not exist."
    exit 1
fi

# Start recording the RSS
echo "Recording RSS for PID $PID every second to $OUTPUT_FILE..."

while kill -0 $PID 2>/dev/null; do
    RSS=$(ps -o rss= -p $PID)
    if [ -z "$RSS" ]; then
        echo "Error: Failed to get RSS for PID $PID."
        exit 1
    fi
    TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
    echo "$TIMESTAMP: $RSS KB" >> $OUTPUT_FILE
    sleep 1
done

echo "Process $PID has terminated. Stopping recording."