Categories :

Raspberry pi – Useful commands

Last Updated on 16/10/2024 by Kevin Agar

Some general commands and some specific to MaxAir

A lot of commands require root access. Either log in with root

su root

password:

This requires root password and changes user to root. It doesn’t change the working directory but stays in the original home directory. So if logged in as pi, you stay in /home/pi

pi@MaxAir:~ $ su root
Password:
root@MaxAir:/home/pi# pwd
/home/pi
root@MaxAir:/home/pi#

Alternatively, you can start all commands with sudo. This gives root permissions and stays in the original home directory. So if logged in as pi, you stay in /home/pi

Rather than type sudo before every command, you can use the command sudo -i This will give root access without the need for a password, but will also change the working directory to /root

sudo -i


root@MaxAir:~# pwd
/root
root@MaxAir:~#

sudo pkill -f gateway.py

#kill the gateway process

sudo ps -aux | grep gateway.py | grep -v grep

#Check gateway is running

sudo python3 /var/www/cron/gateway.py

#Start the gateway in the console window and see the messages_out and messages_in
green = outgoing, yellow = incoming

sudo ps -aux | grep HA_HVAC_integration.py | grep -v grep

#Check that it’s sending data to Home Assistant

systemctl status HA_integration

#Check the status of the HA_integration service

pwd

#print working directory – displays the current directory you’re in

ls

#list a directories content

ls -l

#list a directory’s content with details

cd

#change directory

cd ~

#users home directory. eg /home/pi/

htop

#an interactive process viewer.

sudo python3 /var/www/cron/gateway.py

#run the python program gateway.py

sudo shutdown -r now

#reboot the system

sudo shutdown -h now

#halt the system

sudo nano /etc/ps rc.local 

#edit the file rc.local

sudo cp gateway.py gateway.py.original

#make a file copy

sudo chown www-data:www-data /var/www/cron/gateway.py

#change owner – change file user:group

sudo chown -R www-data:www-data /var/www

#change ownership of all in /var/www recursively

sudo python3 --version

or

sudo python3 -V

#to find what version of python is being used – – is a double dash no space

sudo ps -ef

#list all running processes

sudo ps -ef | grep gateway.py

#show the PID nnn of the running process eg. gateway.py

sudo kill nnn

#kill the PID number nnn to stop the program

sudo raspi-config

#Raspberry pi configuration tool

sudo wpa_cli list_networks

#to see which network is connected

sudo wpa_cli select_network 0 or 1

(from previous command) #To change which network is connected

sudo ifconfig

#show mac address and IP address

sudo df -h

#Show disk space

sudo find / -name file.txt

Breaking this down.

  • find – command
  • sudo – superuser permissions
  • / – directory to run the search (this is the root directory)
  • -name – searching for a file name
  • file.txt – the file we are searching for
sudo find / -type d -name 'httpdocs'

Breaking this down.

  • find – command
  • sudo – superuser permissions
  • / – directory to run the search (this is the root directory)
  • -type what to find
  • d directory
  • -name – searching for a file name
  • ‘httpdocs’ – the directory we are searching for


Increase the swapfile size

sudo nano /etc/dphys-swapfile

#edit the swapfile
CONF_SWAPSIZE=1024 #change swapsize from 100 to 1024

sudo /etc/init.d/dphys-swapfile stop

#stop the swapfile process

sudo /etc/init.d/dphys-swapfile start

#start the swapfile process

free -m   

#check the amount of memory and free memory

sudo vcgencmd get_throttled

# check if the cpu clock is being throttled due to heat. Should return 0

Generate a backup of MaxAir db

run System Configuration Menu>Backup which will generate a file in /var/www eg maxair_mysql_2021-09-22_11-07-52.zip.

Install the MaxAir backup db into clean installation
  1. copy the zip file to your pc and unzip, it should create a *.sql file
  2. install new image
  3. install updates
  4. copy the sql file back to the pi to say /home/pi
  5. on the pi run
  6. sudo mysql -u root -ppassw0rd maxair < ‘the name of the sql file you copied’
  7. eg sudo mysql -u root -ppassw0rd maxair < /home/pi/maxair_mysql_2021_09-22_11-07-52.sql

Raspberry pi – how to measure CPU and GPU Temperature
cd /var/www
sudo nano my-pi-temp.sh

Enter into the file

#!/bin/bash
# Script: my-pi-temp.sh
# Purpose: Display the ARM CPU and GPU  temperature of Raspberry Pi 
# Under GPL v2.x+
# -------------------------------------------------------
cpu=$(</sys/class/thermal/thermal_zone0/temp)
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $(/opt/vc/bin/vcgencmd measure_temp | cut -d = -f2)"
echo "CPU => $((cpu/1000))'C"

Save the file and change the permissions

sudo chmod +x my-pi-temp.sh

To run the file

sudo ./my-pi-temp.sh

Sample output

Fri 24 Sep 21:58:07 BST 2021 @ maxair
-------------------------------------------
GPU => temp=51.5'C
CPU => 51'C
To map a directory to a NAS directory

Create a directory/folder on the pi that you want to map to a directory/folder on the NAS

sudo mkdir /mnt/pibackups

#You need to add the folder to the mnt or media folder as these aren’t included in the backup

Create a directory/folder on the NAS that you want mapped to a directory/folder on the pi

In my case it’s //192.168.1.254/Volume_1/MaxAir_Backups

Add a line to /etc/fstab and save

sudo nano /etc/fstab

Enter into the file and save

//192.168.1.254/Volume_1/Volume_1/MaxAir_Backups /mnt/pibackups cifs nofail,noatime,vers=1.0,username=****,password=****,uid=www-data,gid=www-data 0 0

//192.168.1.254/Volume_1/MaxAir_Backups – NAS address and directory to map to

/mnt/pibackups – MaxAir folder to map from

nofail – System will still boot up even if connecting to the NAS fails

Noatime – Stop changing the file read time. (Improves performance)

vers=1.0 – Used to specify which version of SAMBA is used

username=****,password=**** – The credentials for logging into the NAS

0 0

uid=www-data – set permissions for user www-data

gid=www-data – set permissions for group www-data

Manually Update MaxAir

To manually get the database updates run:

sudo php /var/www/apply_database_update.php

To manually get the code updates run:

sudo python3 /var/www/cron/update_code.py

Then move them to the correct locations using:

sudo python3 /var/www/cron/move_files.py
To check GPIO relays
sudo python3 /var/www/cron/gpio_relay_test.py 13 1

This will turn the relay connected to GPIO pin 13 on

sudo python3 /var/www/cron/gpio_relay_test.py 13 0

This will turn the relay connected to GPIO pin 13 off

To Add MQTT_dependencies

run install.sh from /var/www/add_on/MQTT_dependencies

cd /var/www/add_on/MQTT_dependencies

run

sudo bash ./install.sh

To find, remove or install an earlier paho version
sudo python3 -m pip show paho-mqtt

to find the version

pip3 unistall paho-mqtt

to remove paho

To install an earlier version, create a text file with nano

sudo nano r.txt

add the line

paho-mqtt==1.5.0

Save the file, and in the same directory execute the following

python3 -m pip install -r r.txt

Leave a Reply

Your email address will not be published. Required fields are marked *