在 Raspberry Pi 上使用 DS3231 RTC 的 Systemd 服务
以下 systemd 服务将自动执行。查看此指南了解更多设置信息,并确保 sudo i2cdetect -y 1 检测到地址为 0x68 的 RTC。
这是基于简单 systemd 服务自动安装脚本的自动服务安装和启用脚本。此脚本将在启动时自动启用服务:
install_configure_rtc.sh
#!/bin/bash
# 此脚本安装并启用/启动 systemd 服务
# 它还安装服务文件
export NAME=ConfigureRTC
cat >/etc/systemd/system/${NAME}.service <<EOF
[Unit]
Description=${NAME}
[Service]
ExecStart=/bin/bash -c 'echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device && hwclock -s'
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# 启用并启动服务
systemctl enable --now ${NAME}.serviceinstall_configure_rtc.sh
#!/bin/bash
# 此脚本安装并启用/启动 systemd 服务
# 它还安装服务文件
export NAME=ConfigureRTC
cat >/etc/systemd/system/${NAME}.service <<EOF
[Unit]
Description=${NAME}
[Service]
ExecStart=/bin/bash -c 'echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device && hwclock -s'
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# 启用并启动服务
systemctl enable --now ${NAME}.service这只是 systemd 服务:
configure_rtc.service
[Unit]
Description=ConfigureRTC
[Service]
ExecStart=/bin/bash -c 'echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device && hwclock -s'
Restart=always
[Install]
WantedBy=multi-user.targetconfigure_rtc.service
[Unit]
Description=ConfigureRTC
[Service]
ExecStart=/bin/bash -c 'echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device && hwclock -s'
Restart=always
[Install]
WantedBy=multi-user.targetCheck out similar posts by category:
Linux, Raspberry Pi
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow