0

The main goal of this script is to record the CPU, memory, and temperature of a Raspberry Pi 3 B. I have an outline of my script:

#!/bin/bash

#repeat test 20 times using for loop 
for i in {1...20}
do
#wait half a second 
sleep 0.5

#execute command 1 and call second script

sleep 3

#execute command 2

sleep 3

#execute command 3 

sleep 3

done  

Command 1 is the command that begins gathering the CPU usage. I am doing this by using the sar command found in sysstat tool. I am hoping to execute "sar -P ALL 0.5 >> /home/pi/Desktop/cpu_usage_data.txt". From my understanding and testing of this command, it will record CPU usage of all 4 cores each half second until I tell it to stop storing it into cpu_usage_data.txt which I will later transfer into excel to graph.

Likewise when command 1 is executed I would like to also like to begin recording temperature by calling a secondary script. I did not create the following script but user terdon did. I would like to modify this script to continuously record until the end of the 20 loops.

#!/usr/bin/env bash
vcgencmd measure_temp > /tmp/temp.txt
## Avoid using UPPER CASE variable names since they
## can conflict with environmental variables. 
temp=$(grep -o "[1-9][0-9]\.[0-9]" /tmp/temp.txt)
date=$(date +"%Y-%m-%d %H:%M")
## No need for another variable
printf "%s;%s\n" "$date" "$temp" >> /home/pi/Desktop/temp_hist.csv

Following this, I will wait 3 seconds before executing command 2. Command 2 calls a python program found in path/to/python/program and is executed by doing sudo python3 main.py.

Then wait another 3 seconds and execute command 3, closing the python program. Usually I just exit out of it since it has a GUI but ctrl-c also would work as well.

Then wait another 3 seconds and repeat the test.

By the end I hope to have 2 files, cpu_usage_data.txt and temp_hist.csv filled with data for a period of time. My questions are, how do I execute a command in a script like I would in the terminal? How can I improve my script? Did I miss anything in my outline?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.