Advertisement
mayankjoin3

unix script to compute and save in csv file the ram and cpu consumed by python script input.py every

Mar 7th, 2025
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.82 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Define the CSV file
  4. LOG_FILE="usage_log.csv"
  5.  
  6. # Write the CSV header
  7. echo "Timestamp,CPU_Usage(%),RAM_Usage(MB)" > "$LOG_FILE"
  8.  
  9. # Run the Python script in the background
  10. python input.py &
  11. PID=$!  # Get the process ID of input.py
  12.  
  13. echo "Monitoring process ID: $PID"
  14.  
  15. # Monitor every minute
  16. while kill -0 $PID 2> /dev/null; do
  17.     # Get CPU and RAM usage of the process
  18.     CPU=$(ps -p $PID -o %cpu --no-headers | awk '{print $1}')
  19.     RAM=$(ps -p $PID -o rss --no-headers | awk '{print $1 / 1024}') # Convert KB to MB
  20.    
  21.     # Get the current timestamp
  22.     TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
  23.  
  24.     # Append data to the CSV file
  25.     echo "$TIMESTAMP,$CPU,$RAM" >> "$LOG_FILE"
  26.  
  27.     # Wait for 60 seconds before next reading
  28.     sleep 60
  29. done
  30.  
  31. echo "Process $PID has ended. Monitoring stopped."
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement