树莓派-Ubuntu22.04

树莓派

  • [1 安装Ubuntu系统](#1 安装Ubuntu系统)
  • [2 ssh登录](#2 ssh登录)
  • [3 配置](#3 配置)
    • [3.1 安装软件](#3.1 安装软件)
    • [3.2 换源](#3.2 换源)
    • [3.3 安装桌面](#3.3 安装桌面)
    • [3.4 开机脚本](#3.4 开机脚本)

1 安装Ubuntu系统

通过制作sdk,使系统在sdk中运行:

  1. 下载制作软件:https://www.raspberrypi.com/software/
  2. 下载Ubuntu镜像:https://cn.ubuntu.com/download/raspberry-pi(推荐安装服务器版的)
  3. 格式化sdk
  4. 开始烧录







    等待写入完成。

如果你有屏幕需要去修改屏幕分辨率来进行,重新拔插sdk,找到config文件在文件末尾加上:

800*400分辨率

cpp 复制代码
max_usb_current=1
hdmi_group=2
hdmi_mode=1
hdmi_mode=87
hdmi_cvt 800 480 60 6 0 0 0

1024*600

cpp 复制代码
max_usb_current=1
hdmi_group=2
hdmi_mode=1
hdmi_mode=87
hdmi_cvt 1024 600 60 6 0 0 0

2 ssh登录

  1. 扫描IP地址:工具:https://www.advanced-ip-scanner.com/ 找到树莓派的IP地址
  2. 下载ssh建立连接即可

3 配置

3.1 安装软件

  1. 网络工具
bash 复制代码
sudo apt-get install net-tools

3.2 换源

清华源:https://mirror.tuna.tsinghua.edu.cn/help/ubuntu-ports/

备份:sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

编辑:sudo vi /etc/apt/sources.list

注释或者删掉里面的内容,将下面的内容复制进去

bash 复制代码
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse

deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse

更新:

bash 复制代码
sudo apt-get update
sudo apt-get upgrade

3.3 安装桌面

选择轻量级的桌面lxde

  1. 更新软件和系统
bash 复制代码
sudo apt update
sudo apt upgrade
  1. 安装桌面
bash 复制代码
sudo apt install lxde
#Xorg 是提供底层图形支持的服务器,而 xinit(通过 startx)是一种启动和使用这个服务器的方法
sudo apt install xorg
sudo apt install xinit
  1. 启动(通过ssh链接的看不到)
bash 复制代码
startx

3.4 开机脚本

  1. systemd服务
    systemd服务默认是在没有图形界面环境的背景下运行的

创建脚本/usr/local/bin/ss.sh:

bash 复制代码
#!/bin/bash
# 你的脚本内容
echo "Hello, World!" > /tmp/hello.txt

给予脚本执行权限:

bash 复制代码
sudo chmod +x /usr/local/bin/ss.sh

创建systemd服务文件

接下来,创建一个systemd服务文件来执行脚本。服务文件通常位于/etc/systemd/system/目录下。以管理员权限创建一个新的服务文件,例如my-startup-service.service:

sudo nano /etc/systemd/system/my-startup-service.service

在打开的编辑器中,添加以下内容:

bash 复制代码
[Unit]
Description=My custom startup script

[Service]
Type=simple
ExecStart=/usr/local/bin/ss.sh

[Install]
WantedBy=multi-user.target

这定义了一个新的服务,它在系统启动时执行您的脚本。Description字段是对服务的描述,ExecStart指定了脚本的完整路径。

启用并启动服务

保存并关闭服务文件后,重新加载systemd以识别新服务,并启用此服务以在启动时运行

bash 复制代码
sudo systemctl daemon-reload
sudo systemctl enable my-startup-service.service

如果您想立即测试服务而不重启,可以使用以下命令启动服务:

bash 复制代码
sudo systemctl start my-startup-service.service

验证服务状态

运行以下命令以检查服务的状态:

bash 复制代码
sudo systemctl status my-startup-service.service

这将显示服务的当前状态和最近的日志输出。如果一切配置正确,您的脚本应该会在每次系统启动时自动执行。

通过这种方式,您可以编写和配置任何类型的开机脚本,无论是进行系统初始化操作,还是启动特定的应用程序或服务。

  1. 使用 Cron

cron 是一个时间基的作业调度器,在 Linux 系统中用于定时执行任务。对于在启动时运行脚本,可以使用 @reboot 指令。

创建脚本ss.sh

bash 复制代码
#!/bin/bash
# 你的脚本内容
#QT环境
export PATH=/opt/Qt5.14.2/5.14.2/gcc_64/bin:$PATH
export LD_LIBRARY_PATH=/opt/Qt5.14.2/5.14.2/gcc_64/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=/opt/Qt5.14.2/5.14.2/gcc_64/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/opt/Qt5.14.2/5.14.2/gcc_64/qml:$QML2_IMPORT_PATH
#默认显示器
export DISPLAY=:0
#需要启动的程序
cd /opt/serialTool/
./Serial
echo "end!" > /home/mql/桌面/1.txt

打开当前用户的 crontab 文件进行编辑:

bash 复制代码
crontab -e

在文件末尾添加以下行以在每次启动时运行脚本:

bash 复制代码
#@reboot /path/to/your/ss.sh
#延时执行单位s
@reboot sleep 5 && /path/to/your/ss.sh
#打印日志
@reboot /path/to/your/ss.sh >> /home/mql/桌面/2.txt 2>&1

替换 /path/to/your/script.sh 为您的脚本实际路径。

保存并退出编辑器。

重启就可以执行脚本,可以根据延时启动相应带有图形界面的软件

相关推荐
astuv2 天前
在树莓派上部署开源监控系统 ZoneMinder
linux·nginx·树莓派·监控·摄像头·zoneminder·apache2
天飓19 天前
树莓派智能语音助手实现音乐播放
人工智能·python·语音识别·树莓派·rasa·sounddevice
小手智联老徐20 天前
MATLAB 从 R2024B 开始支持树莓派 5
嵌入式硬件·matlab·树莓派
极客小张1 个月前
构建智能门禁安防系统:树莓派 4B、OpenCV、SQLite 和 MQTT 的应用(代码示例)
jvm·单片机·opencv·物联网·计算机视觉·sqlite·树莓派
天飓1 个月前
让自家的智能语音助手实现todo任务的添加
python·树莓派·rasa·todo list
上海晶珩EDATEC1 个月前
基于树莓派的儿童音频播发器—Yoto
树莓派·raspberry pi·树莓派5·raspberry pi 5·工业计算机
上海晶珩EDATEC1 个月前
Vodafone 推出了与 Wi-Fi 竞争的基于树莓派私人5G技术
5g·树莓派·raspberry pi·树莓派5·raspberry pi 5
极客小张2 个月前
使用树莓派Raspberry Pi 4和OpenWrt构建Web界面的嵌入式路由器:全面技术栈解析(代码示例)
前端·javascript·物联网·网络协议·flask·智能路由器·树莓派
少年、潜行2 个月前
树莓派开发笔记03-树莓派的GPIO口输入检测
笔记·嵌入式·树莓派·3b+
小草cys2 个月前
树莓派neo4j在noe4j中将csv文件添加节点
neo4j·树莓派·合并