feat(bin): Add size-track and process-track-name scripts

This commit is contained in:
2025-11-10 08:10:44 +01:00
parent 673e14847b
commit a1a9aae73e
3 changed files with 91 additions and 0 deletions

3
.gitignore vendored
View File

@@ -44,6 +44,9 @@
!bin/vpn
!bin/eduroam_connect
!bin/size-track
!bin/process-track-name
!share
!share/larbs
!share/fonts

60
bin/process-track-name Executable file
View File

@@ -0,0 +1,60 @@
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 <program_name|--all>"
echo " program_name: track a specific program"
echo " --all: track all processes system-wide"
exit 1
fi
PROGRAM_NAME=$1
OUTPUT_FILE="process_stats.log"
TRACK_ALL=false
if [ "$PROGRAM_NAME" = "--all" ]; then
TRACK_ALL=true
OUTPUT_FILE="all_process_stats.log"
fi
touch $OUTPUT_FILE
if [ "$TRACK_ALL" = true ]; then
echo "tracking system-wide CPU and memory usage..."
echo "date time,total cpu %, total memory %" >> $OUTPUT_FILE
while true; do
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
TOTAL_CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//')
TOTAL_MEM=$(free | grep Mem | awk '{printf "%.1f", ($3/$2) * 100.0}')
echo "$TIMESTAMP,$TOTAL_CPU,$TOTAL_MEM" >> $OUTPUT_FILE
sleep 1
done
else
echo "date time,pid,total cpu %, total memory %" >> $OUTPUT_FILE
while true; do
PIDS=$(pgrep -x $PROGRAM_NAME | grep -v $$ | grep -v $(pgrep -x pgrep))
if [ -n "$PIDS" ]; then
PID=$(echo $PIDS | awk '{print $1}')
echo "found process $PROGRAM_NAME with PID $PID"
while kill -0 $PID 2>/dev/null; do
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
echo -n "$TIMESTAMP," >> $OUTPUT_FILE
ps -p $PID -o pid,%cpu,%mem,cmd --no-headers >> $OUTPUT_FILE
sleep 1
done
echo "Process $PROGRAM_NAME with PID $PID has terminated"
exit 0
fi
sleep 1
done
fi

28
bin/size-track Executable file
View File

@@ -0,0 +1,28 @@
#!/bin/bash
DIR="$1"
LOGFILE="$2"
if [[ -z "$DIR" || -z "$LOGFILE" ]]; then
echo "Usage: $0 /path/to/directory /path/to/logfile.log"
exit 1
fi
if [[ ! -d "$DIR" ]]; then
echo "'$DIR': not found"
exit 1
fi
echo "logging size of $DIR to $LOGFILE every 5 seconds (in KB)."
echo "timestamp,directory size (KB),free space left (KB),total space (KB)" | tee -a "$LOGFILE"
while true; do
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
DIR_SIZE=$(du -sk "$DIR" 2>/dev/null | awk '{print $1}')
DF_OUTPUT=$(df -k "$DIR" | awk 'NR==2')
TOTAL_SPACE=$(echo "$DF_OUTPUT" | awk '{print $2}')
FREE_SPACE=$(echo "$DF_OUTPUT" | awk '{print $4}')
LOG_LINE="$TIMESTAMP,$DIR_SIZE,$FREE_SPACE,$TOTAL_SPACE"
echo "$LOG_LINE" | tee -a "$LOGFILE"
sleep 5
done