Created
April 9, 2025 10:10
-
-
Save bsitruk/b300dee364dba796919ff1a176faa968 to your computer and use it in GitHub Desktop.
Monitor Process (RAM, CPU, FD, Thread Count)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check for correct number of arguments | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 <PID> <Label>" | |
exit 1 | |
fi | |
PID=$1 | |
LABEL=$2 | |
FILE="process_stats_${LABEL}.csv" | |
# Add CSV header | |
echo "Timestamp, RAM (KB), CPU (%), Threads_Count, FD_Count" > $FILE | |
# Monitoring function | |
monitor_process() { | |
while true; do | |
TIMESTAMP=$(date +%Y-%m-%d\ %H:%M:%S) | |
RAM=$(ps -p $PID -o rss=) | |
CPU=$(ps -p $PID -o %cpu=) | |
THREADS_COUNT=$(ps -p $PID -o nlwp=) | |
FD_COUNT=$(ls /proc/$PID/fd | wc -l) | |
# Append to CSV | |
echo "$TIMESTAMP, $RAM, $CPU, $THREADS_COUNT, $FD_COUNT" >> $FILE | |
# Update every second | |
sleep 1 | |
done | |
} | |
# Check if process exists | |
if kill -0 $PID 2>/dev/null; then | |
monitor_process | |
else | |
echo "Process with PID $PID not found." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment