Created
December 28, 2023 20:09
-
-
Save ocodista/a6f289fb73dbc1f8c309a169bc8cd38c to your computer and use it in GitHub Desktop.
Bash script to monitor process and save to .csv
This file contains 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