User Tools

Site Tools


setup_weeder.sh

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
setup_weeder.sh [2025/12/20 15:04] 46.109.187.144setup_weeder.sh [2026/05/29 07:14] (current) 46.109.187.144
Line 1: Line 1:
 +**chmod +x setup_weeder.sh && sudo ./setup_weeder.sh**
 +
 +<code>
 #!/bin/bash #!/bin/bash
 +set -euo pipefail
 +
 +# ====== CONFIG ======
 +USER="pi"
 +HOME_DIR="/home/$USER"
 +APP_DIR="$HOME_DIR/Desktop/WEEDER_PREMIUM_MULTI"
 +SERVICE_NAME="weeder.service"
 +
 +echo "=== Install & enable NetworkManager ==="
 +sudo apt update || true
 +sudo apt install -y network-manager openssh-server
 +echo "=== Disable background updates / indexing / notifications ==="
 +
 +# Keep correct time sync ON
 +sudo timedatectl set-timezone Europe/Riga || true
 +sudo timedatectl set-ntp true || true
 +
 +# Disable apt automatic update timers/services
 +sudo systemctl disable --now apt-daily.timer apt-daily-upgrade.timer 2>/dev/null || true
 +sudo systemctl disable --now apt-daily.service apt-daily-upgrade.service 2>/dev/null || true
 +sudo systemctl mask apt-daily.timer apt-daily-upgrade.timer apt-daily.service apt-daily-upgrade.service 2>/dev/null || true
 +
 +# Disable unattended upgrades
 +sudo systemctl disable --now unattended-upgrades.service 2>/dev/null || true
 +sudo systemctl mask unattended-upgrades.service 2>/dev/null || true
 +
 +# Disable PackageKit / GUI package refresh
 +sudo systemctl disable --now packagekit.service packagekit-offline-update.service 2>/dev/null || true
 +sudo systemctl mask packagekit.service packagekit-offline-update.service 2>/dev/null || true
 +
 +# Disable update notifier
 +sudo systemctl disable --now update-notifier-download.timer update-notifier-download.service 2>/dev/null || true
 +sudo systemctl mask update-notifier-download.timer update-notifier-download.service 2>/dev/null || true
 +
 +sudo systemctl disable --now update-notifier-motd.timer update-notifier-motd.service 2>/dev/null || true
 +sudo systemctl mask update-notifier-motd.timer update-notifier-motd.service 2>/dev/null || true
 +
 +# Disable plocate daily database indexing
 +sudo systemctl disable --now plocate-updatedb.timer plocate-updatedb.service 2>/dev/null || true
 +sudo systemctl mask plocate-updatedb.timer plocate-updatedb.service 2>/dev/null || true
 +
 +# Disable firmware update daemon refresh
 +sudo systemctl disable --now fwupd-refresh.timer fwupd-refresh.service 2>/dev/null || true
 +sudo systemctl mask fwupd-refresh.timer fwupd-refresh.service 2>/dev/null || true
 +
 +# Disable anacron daily jobs
 +sudo systemctl disable --now anacron.timer anacron.service 2>/dev/null || true
 +sudo systemctl mask anacron.timer anacron.service 2>/dev/null || true
 +
 +# Disable snap background refresh/services if you do not need snap apps
 +sudo systemctl disable --now snapd.service snapd.socket snapd.seeded.service 2>/dev/null || true
 +sudo systemctl mask snapd.service snapd.socket snapd.seeded.service 2>/dev/null || true
 +
 +# Disable printing services if printer support is not needed
 +sudo systemctl disable --now cups.service cups.socket cups.path cups-browsed.service 2>/dev/null || true
 +sudo systemctl mask cups.service cups.socket cups.path cups-browsed.service 2>/dev/null || true
 +
 +# Disable GNOME tracker file indexing for user pi
 +sudo -u pi systemctl --user stop tracker-miner-fs-3.service tracker-extract-3.service 2>/dev/null || true
 +sudo -u pi systemctl --user mask tracker-miner-fs-3.service tracker-extract-3.service 2>/dev/null || true
  
-Lietotājvārds +Kill already running background processes 
-USER=pi +pkill -f tracker-miner-fs-3 2>/dev/null || true 
-HOME_DIR=/home/$USER+pkill -f update-notifier 2>/dev/null || true 
 +pkill -f apt-check 2>/dev/null || true 
 +pkill -f snap-store 2>/dev/null || true 
 +pkill -f snapd-desktop-integration 2>/dev/null || true
  
-sudo apt install -y network-manager+# Disable APT periodic config 
 +sudo bash -c 'cat > /etc/apt/apt.conf.d/20auto-upgrades <<EOF 
 +APT::Periodic::Update-Package-Lists "0"; 
 +APT::Periodic::Download-Upgradeable-Packages "0"; 
 +APT::Periodic::AutocleanInterval "0"; 
 +APT::Periodic::Unattended-Upgrade "0"; 
 +EOF'
 sudo systemctl enable --now NetworkManager sudo systemctl enable --now NetworkManager
 +sudo systemctl enable --now ssh
  
-# 0. Statiskā IP konfigurācija ar netplan +echo "=== Netplan: NetworkManager renderer + static eth0 (link-local) ===" 
-sudo bash -c "cat <<EOF > /etc/netplan/50-weeder-static-ip.yaml+sudo bash -c "cat <<'EOF> /etc/netplan/99-nm.yaml
 network: network:
   version: 2   version: 2
Line 16: Line 89:
     eth0:     eth0:
       dhcp4: no       dhcp4: no
-      addresses: [169.254.1.244/24] +      addresses: 
-      routes: +        - 169.254.1.244/24 
-        - to: default +EOF" 
-          via: 169.254.1.1 +sudo netplan apply
-      nameservers: +
-        addresses: [8.8.8.8, 1.1.1.1]+
  
 +echo "=== Create systemd service: $SERVICE_NAME ==="
 +sudo bash -c "cat <<EOF > /etc/systemd/system/$SERVICE_NAME
 +[Unit]
 +Description=Weeder Premium Multi 2 startup script
 +Wants=network-online.target
 +After=network-online.target
 +
 +[Service]
 +Type=simple
 +User=$USER
 +WorkingDirectory=$APP_DIR
 +ExecStart=/usr/bin/python3 $APP_DIR/server.py
 +Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 +Restart=always
 +RestartSec=5
 +StandardOutput=append:$APP_DIR/startup.log
 +StandardError=append:$APP_DIR/startup_error.log
 +
 +[Install]
 +WantedBy=multi-user.target
 EOF" EOF"
  
-# Piemēro netplan konfigurāciju +echo "=== Enable and start service ===" 
-sudo netplan apply+sudo systemctl daemon-reload 
 +sudo systemctl enable "$SERVICE_NAME" 
 +sudo systemctl restart "$SERVICE_NAME"
  
-# 1. Izveido .desktop saīsni +echo "=== Desktop shortcut (optional) ===" 
-cat <<EOF > $HOME_DIR/Desktop/weeder.desktop+mkdir -p "$HOME_DIR/Desktop" 
 +cat <<EOF > "$HOME_DIR/Desktop/weeder.desktop"
 [Desktop Entry] [Desktop Entry]
 Version=1.0 Version=1.0
 Name=weeder Name=weeder
-Comment=Run my Python script +Comment=Run WEEDER server 
-Exec=python3 $HOME_DIR/Desktop/WEEDER_PREMIUM_MULTI/server.py+Exec=/usr/bin/python3 $APP_DIR/server.py
 Terminal=true Terminal=true
 Type=Application Type=Application
-Category=Utility;+Categories=Utility;
 EOF EOF
 +gio set "$HOME_DIR/Desktop/weeder.desktop" metadata::trusted true || true
 +chmod +x "$HOME_DIR/Desktop/weeder.desktop"
 +chown "$USER:$USER" "$HOME_DIR/Desktop/weeder.desktop" || true
  
-# 2Piešķir uzticamību un izpildes tiesības +echo "=== Sudoers (safe) ===" 
-gio set $HOME_DIR/Desktop/weeder.desktop metadata::trusted true +sudo bash -c "cat <<EOF > /etc/sudoers.d/weeder 
-chmod +x $HOME_DIR/Desktop/weeder.desktop+$USER ALL=(ALL) NOPASSWD: /sbin/shutdown, /sbin/iwlist, /usr/bin/nmcli, /bin/dmesg 
 +EOF" 
 +sudo chmod 0440 /etc/sudoers.d/weeder 
 + 
 +echo "=== Setup complete. Rebooting in 5 seconds... ===" 
 +sleep 5 
 +sudo reboot 
 + 
 + 
 +</code> 
 + 
 + 
 +<code> 
 +#!/bin/bash 
 +set -euo pipefail 
 + 
 +# ====== CONFIG ====== 
 +USER="pi" 
 +HOME_DIR="/home/$USER" 
 +APP_DIR="$HOME_DIR/Desktop/WEEDER_PREMIUM_MULTI" 
 +SERVICE_NAME="server.service" 
 +USB_MOUNT="/mnt/weederusb" 
 +USB_IMG_DIR="$USB_MOUNT/field_images" 
 +PATH_STORAGE="$APP_DIR/data_storage/path_storage.json" 
 + 
 +echo "=== Install & enable NetworkManager ===" 
 +sudo apt update || true 
 +sudo apt install -y network-manager openssh-server 
 +sudo systemctl enable --now NetworkManager 
 +sudo systemctl enable --now ssh 
 + 
 +echo "=== NetplanNetworkManager renderer + static eth0 (link-local) ===" 
 +sudo bash -c "cat <<'EOF' > /etc/netplan/99-nm.yaml 
 +network: 
 +  version: 2 
 +  renderer: NetworkManager 
 +  ethernets: 
 +    eth0: 
 +      dhcp4: no 
 +      addresses: 
 +        - 169.254.1.244/16 
 +      routes: 
 +        - to: default 
 +          via: 169.254.1.1 
 +EOF" 
 + 
 +sudo netplan apply || true 
 + 
 +echo "=== Prepare USB mount folder ===" 
 +sudo mkdir -p "$USB_MOUNT" 
 +sudo chown "$USER:$USER" "$USB_MOUNT" 
 +sudo chmod 777 "$USB_MOUNT" 
 + 
 +echo "=== Update path_storage.json img_folder_path ===" 
 +if [ -f "$PATH_STORAGE" ]; then 
 +python3 - <<PY 
 +import json 
 + 
 +path = "$PATH_STORAGE" 
 + 
 +with open(path, "r") as f: 
 +    data = json.load(f) 
 + 
 +data["img_folder_path"] = "$USB_IMG_DIR/"
  
-# 3. Pievienot /sbin/iwlist un /usr/bin/nmcli bez parolēm +with open(path, "w"as f: 
-echo "$USER ALL=(ALLNOPASSWD/sbin/shutdown/sbin/iwlist/usr/bin/nmcli" | sudo tee -a /etc/sudoers+    json.dump(datafindent=4)
  
-# 4. Pievienot /bin/dmesg bez parolēm +print("img_folder_path set to:", data["img_folder_path"]) 
-echo "$USER ALL=(ALL) NOPASSWD/bin/dmesg| sudo tee -a /etc/sudoers+PY 
 +else 
 +    echo "WARNINGpath_storage.json not found: $PATH_STORAGE" 
 +fi
  
-# 5. Izveidot systemd servisu automātiskai palaišanai +echo "=== Create systemd service: $SERVICE_NAME ===" 
-sudo bash -c "cat <<EOF > /etc/systemd/system/my_script.service+sudo bash -c "cat <<EOF > /etc/systemd/system/$SERVICE_NAME
 [Unit] [Unit]
-Description=WEEDER code +Description=Weeder Premium Multi server startup script 
-After=network.target+Wants=network-online.target 
 +After=network-online.target
  
 [Service] [Service]
-ExecStart=/usr/bin/python3 $HOME_DIR/Desktop/WEEDER_PREMIUM_MULTI/server.py +Type=simple
-Environment=\"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\" +
-WorkingDirectory=$HOME_DIR/Desktop/WEEDER_PREMIUM_MULTI +
-StandardOutput=journal +
-StandardError=journal +
-Restart=on-failure +
-RestartSec=2+
 User=$USER User=$USER
 +WorkingDirectory=$APP_DIR
 +
 +# Create stable USB mount point
 +ExecStartPre=/bin/mkdir -p $USB_MOUNT
 +
 +# Unmount old mount if exists. '-' means ignore error.
 +ExecStartPre=-/bin/umount $USB_MOUNT
 +
 +# Find first USB storage partition and mount it to $USB_MOUNT.
 +# If USB exists, mount with pi permissions.
 +# If no USB exists, service still starts and uses local /mnt/weederusb folder.
 +ExecStartPre=/bin/bash -c 'USB_DEV=\$(/bin/lsblk -rpno NAME,TYPE,TRAN | /usr/bin/awk '\\''\$2=="part" && \$3=="usb" {print \$1; exit}'\\''); if [ -n "\$USB_DEV" ]; then echo "[server.service] USB found: \$USB_DEV"; /bin/mount -o uid=1000,gid=1000,umask=000 "\$USB_DEV" $USB_MOUNT || /bin/mount "\$USB_DEV" $USB_MOUNT; else echo "[server.service] No USB found, using local $USB_MOUNT"; fi'
 +
 +# Create image folder
 +ExecStartPre=/bin/mkdir -p $USB_IMG_DIR
 +ExecStartPre=/bin/chmod -R 777 $USB_IMG_DIR
 +
 +ExecStart=/usr/bin/python3 $APP_DIR/server.py
 +
 +Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 +Restart=always
 +RestartSec=5
 +StandardOutput=append:$APP_DIR/startup.log
 +StandardError=append:$APP_DIR/startup_error.log
  
 [Install] [Install]
Line 70: Line 252:
 EOF" EOF"
  
-# 6Aktivēt servisu+echo "=== Disable old weeder.service if it exists ===" 
 +sudo systemctl disable weeder.service 2>/dev/null || true 
 +sudo systemctl stop weeder.service 2>/dev/null || true 
 + 
 +echo "=== Enable and start $SERVICE_NAME ==="
 sudo systemctl daemon-reload sudo systemctl daemon-reload
-sudo systemctl enable my_script.service +sudo systemctl enable "$SERVICE_NAME" 
-sudo systemctl start my_script.service+sudo systemctl restart "$SERVICE_NAME" 
 + 
 +echo "=== Desktop shortcut (optional) ===" 
 +mkdir -p "$HOME_DIR/Desktop" 
 +cat <<EOF > "$HOME_DIR/Desktop/weeder.desktop" 
 +[Desktop Entry] 
 +Version=1.0 
 +Name=weeder 
 +Comment=Run WEEDER server 
 +Exec=/usr/bin/python3 $APP_DIR/server.py 
 +Terminal=true 
 +Type=Application 
 +Categories=Utility; 
 +EOF 
 + 
 +gio set "$HOME_DIR/Desktop/weeder.desktop" metadata::trusted true || true 
 +chmod +x "$HOME_DIR/Desktop/weeder.desktop" 
 +chown "$USER:$USER" "$HOME_DIR/Desktop/weeder.desktop" || true 
 + 
 +echo "=== Sudoers (safe) ===" 
 +sudo bash -c "cat <<EOF > /etc/sudoers.d/weeder 
 +$USER ALL=(ALL) NOPASSWD: /sbin/shutdown, /usr/sbin/shutdown, /usr/bin/systemctl, /sbin/iwlist, /usr/bin/nmcli, /bin/dmesg, /usr/bin/dmesg, /bin/mount, /usr/bin/mount, /bin/umount, /usr/bin/umount 
 +EOF" 
 + 
 +sudo chmod 0440 /etc/sudoers.d/weeder
  
-echo " Iestatīšana pabeigta. Raspberry Pi tiks pārstartēts pēc sekundēm..."+echo "=== Setup complete ===" 
 +echo "Service name: $SERVICE_NAME" 
 +echo "Image path: $USB_IMG_DIR/" 
 +echo "" 
 +echo "Check with:" 
 +echo "  systemctl status $SERVICE_NAME" 
 +echo "  journalctl -u $SERVICE_NAME -f" 
 +echo "  mount | grep weederusb" 
 +echo "  grep -R 'img_folder_path' $PATH_STORAGE" 
 +echo "" 
 +echo "Rebooting in seconds..."
 sleep 5 sleep 5
 sudo reboot sudo reboot
 +</code>
  
 +sudo systemctl disable --now apt-daily.timer
 +sudo systemctl disable --now apt-daily-upgrade.timer
 +sudo systemctl disable --now unattended-upgrades.service
 +sudo systemctl disable --now packagekit.service
setup_weeder.sh.1766243064.txt.gz · Last modified: by 46.109.187.144 · Currently locked by: 216.73.217.69