Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Define the CSV file
- LOG_FILE="usage_log.csv"
- # Write the CSV header
- echo "Timestamp,CPU_Usage(%),RAM_Usage(MB)" > "$LOG_FILE"
- # Run the Python script in the background
- python input.py &
- PID=$! # Get the process ID of input.py
- echo "Monitoring process ID: $PID"
- # Monitor every minute
- while kill -0 $PID 2> /dev/null; do
- # Get CPU and RAM usage of the process
- CPU=$(ps -p $PID -o %cpu --no-headers | awk '{print $1}')
- RAM=$(ps -p $PID -o rss --no-headers | awk '{print $1 / 1024}') # Convert KB to MB
- # Get the current timestamp
- TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
- # Append data to the CSV file
- echo "$TIMESTAMP,$CPU,$RAM" >> "$LOG_FILE"
- # Wait for 60 seconds before next reading
- sleep 60
- done
- echo "Process $PID has ended. Monitoring stopped."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement