Raspberry Pi Notes

Useful Commands

sudo shutdown -h 0
sudo reboot -h 0

Dealing with USB drives

df -h
sudo fisk -l

Process Monitor

ps
ps -u pi
ps -A

Update and Upgrade packages

sudo apt update
sudo apt full-upgrade

Move /tmp to RAM

sudo nano /etc/fstab

tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=100m 0 0
tmpfs /tmp tmpfs defaults,noatime,nosuid,size=100m 0 0
tmpfs /var/tmp tmpfs defaults,noatime,nosuid,size=30m 0 0
tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=100m 0 0
tmpfs /var/run tmpfs defaults,noatime,nosuid,mode=0755,size=2m 0 0
tmpfs /var/spool/mqueue tmpfs defaults,noatime,nosuid,mode=0700,gid=12,size=30m 0 0

Glances Monitor
Glances does a very comprehensive job and is ideal for headless, GUI-less RPis as it just runs a daemon on the RPi and the output is viewed on another machine on the same LAN. You install the Python package and then can run it in background as below.
https://linuxhint.com/monitor-raspberry-pi-performance-remotely-using-glances/

sudo pip3 install bottle
sudo pip3 install glances
glances -w &

Make a note of the PID as later you may want to stop the process with KILL {PID}
Now view on a browser:-
http://{your-rpi-ip-address}:61208

RealVNC : Remote Desktop

So easy to implement! You don’t need an account to sign-in if you are just using it as a remote desktop on your LAN. It isn’t encrypted in this scenario. The graphics and speed are very good.

sudo raspi-config

interfacing options > enable VNC
On remote (Windows PC): download and install RealVNC Viewer from https://www.realvnc.com/en/connect/download/viewer/

Install AnyDesk

download deb from Anydesk
sudo dpkg -i anydesk_6.3.0-1_armhf.deb
sudor apt install -f
sudo systemctl status anydesk
sudo apt install libgles-dev libegl-dev
ls /usr/lib/arm-linux-gnueabihf/libGLE*
sudo ln -s /usr/lib/arm-linux-gnueabihf/libGLESv2.so /usr/lib/libbrcmGLESv2.so
ls /usr/lib/arm-linux-gnueabihf/libEGL*
sudo ln -s /usr/lib/arm-linux-gnueabihf/libEGL.so /usr/lib/libbrcmEGL.so
cd Downloads/
systemctl daemon-reload
sudo apt-get install -f
sudo systemctl status anydesk

Speed up the mouse

First try Preferences > Mouse and Keyboard Settings and if that doesn’t work try this. Note that polling numbers go 1,2,4,8 getting half as slow as the number rises.

sudo nano /boot/cmdline.txt
now add a space and the following to the end of the top line:-
 usbhid.mousepoll=4

Install and use PiShrink

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
sudo chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin

Backup

  • Boot RPi with “backup system”
  • Insert the output USB stick into a USB3 port as SDA
  • Insert the input drive into a card reader in USB3 port as SDB
  • Check drives are correct and sizes with FDISK
  • Could use Gparted to shrink rootfs on MicroSD (comment left in just in case)
sudo fdisk -l
sudo fdisk -s /dev/sda2
  • Assuming just under 8Gb, lets go with 8000 1MB sectors:-
sudo mkdir /dev/mysub 
sudo mount /dev/sda1 /dev/mysub
sudo dd if=/dev/sdb of=/dev/mysub/my.img bs=1M count=8000  [took about 7mins]
cd /dev/mysub sudo pishrink.sh -z my.img [took about 10mins to shrink to about 30%]
  • Dismount everything and put the card reader into Win PC
  • Run Raspberry Pi Imager and Choose OS – use custom
  • Used new micro SD card and boot in original Rpi

RaspiBackup

Assuming backup to NTFS partition on my 128Gb USB Flash Drive this is sda2

sudo mkdir /backup
sudo mount /dev/sda2 /backup
sudo blkid -o list
mount | grep backup
sudo raspiBackup -t tar -m detailed
sudo umount /dev/sda2

With this flash drive it takes about 4mins to produce a 4Gb tar

Turn off LEDs

sudo nano /boot/config.txt

# Turn off PWR LED
dtparam=pwr_led_trigger=default-on
dtparam=pwr_led_activelow=off

# Turn off ACT LED
dtparam=act_led_trigger=none
dtparam=act_led_activelow=off

# Turn off Ethernet ACT LED
dtparam=eth_led0=4

# Turn off Ethernet LNK LED
dtparam=eth_led1=4

Show Line numbers in Nano

nano ~/.nanorc
set constantshow

Turn off Wifi and Bluetooth

sudo nano /boot/config.txt

dtoverlay=disable-wifi
dtoverlay=disable-bt

Cache Chromium with RAM Disk

sudo nano /etc/fstab

tmpfs           /media/ramdisk  tmpfs   defaults,noatime,mode=1777 0 0

Ensure Chromium is closed and then open its properties replacing the command on the Desktop tab…

chromium-browser %U --disk-cache-dir="/media/ramdisk/"

Bridge from another computer with Wifi using Ethernet

I tried sharing the wi-fi adaptor on the PC and specifying ip info on the ethernet adaptor to no avail. Bridging didn’t seem to work either. I switched on IPv6 LAN addressing on the router with bridging and that seemed to make IPv4 work – confusing! However, this is the working set up:-

  1. Router has IPv6 switched on
  2. RPi has Wifi switched off
  3. PC – bridge Wifi adaptor
  4. PC Bridge Properties
    1. ensure that both Ethernet and Wifi adaptors are ticked
    2. in IPv4 properties ensure “obtain an IP address automatically” is ticked (and DNS server address too).

Start process in Pi user at boot time
sudo nano /etc/rc.local
add just before the exit 0 …

su sq -c "python3 myscript.py &"

Var/Log tweaks
sudo nano /etc/rsyslog.conf

Stop ICMPv6 advertisement noise by adding this line immediately after the #RULES section

:msg, contains, "advertised our address" stop

To send cron job info messages to cron.log

*.=info;*.=notice;*.=warn;\
cron -/var/log/cron.log

Restart rsyslog with

sudo systemctl restart rsyslog.service && systemctl status rsyslog.service

Pipe output to file 

Put this after the command

eg.

replace file and write to mylog.log

1>mylog.log

send std and errors to the log

1>mylog.log 2>&1

append rather than replace the log file

1>>mylog.log 2>&1

Pipe Cron output to /var/log

crontab -e

1. Turn off “No MTA installed, discarding output” messages in the logs.  This is because there isn’t a MAILTO set so
Add this row…

MAILTO = ""

2. Pipe Cron sysout and syserr output to the logger and tell it to log info messages
Append the Cron command with this

 2>&1 | /usr/bin/logger -p local3.info