termux-services

termux-services contains a set of scripts for controlling services. Instead of putting commands in ~/.bashrc or ~/.bash_profile, they can be started and stopped with termux-services.

Supported services
PackageDaemonPortDescription
apache2httpd8080Apache Web Server HTTP Web Server
atatdAT and batch delayed command scheduling utility and daemon at
bitcoinbitcoindBitcoin Core
busyboxtelnetd8023Remote terminal service Telnet
busyboxftpd8021FTP (File Transfer Protocol) FTP
croniecrondDaemon that runs specified programs at scheduled times cron
emacsemacsdExtensible, customizable text editor-and more EMACS
ipfsipfsA peer-to-peer hypermedia distribution protocol IPFS
libmosquittomosquitto1883MQTT is a lightweight, publish-subscribe network protocol MQTT
lighttpdlighttpd8080Lighttpd Small webserver HTTP Web Server
lndlndLightning Network Daemon
mariadbmysqld3306MariaDB Community-developed fork of the MySQL from its original authors
mpdmpdMusic Player Daemon (MPD)
mpdscribblempdscribbleMPD client which submits tracks being played
nginxnginx8080NGINX Web server HTTP Web Server
opensshsshd8022OpenSSH SSH
postgresqlpostgres5432PostgreSQL database PostgreSQL
privoxyprivoxyPrivoxy is non-caching web proxy with advanced filtering capabilities HTTP Proxy
tortorThe Onion Router anonymizing overlay network Tor
transmissiontransmissionBitTorrent client BitTorrent


To install termux-services, run

pkg install termux-services

and then restart termux so that the service-daemon is started.

To then enable and run a service, run

sv-enable <service>

If you only want to run it once, run

sv up <service>

To later stop a service, run:

sv down <service>

Or to disable it

sv-disable <service>

A service is disabled if `$PREFIX/var/service/<service>/down` exists, so the `sv-enable` and `sv-disable` scripts touches, or removes, this file.

termux-services uses the programs from runit to control the services. A bunch of example scripts are available from the same site. If you find a script you want to use, or if you write your own, you can use set it up by running:

mkdir -p $PREFIX/var/service/<PKG>/log
ln -sf $PREFIX/share/termux-services/svlogger $PREFIX/var/service/<PKG>/log/run

and then put your run script for the package at $PREFIX/var/service/<PKG>/run and make sure that it is runnable.

You can then run

sv up <PKG>

to start it.

Log files for services are situated in $PREFIX/var/log/sv/<PKG>/ with the active log file named "current".

Starting termux-services on boot

If you want your services to start on device boot, see the example in Termux:Boot

<think>好的,我现在要帮用户介绍如何在Termux上搭建智能家居控制中心。首先,我需要考虑用户可能的需求。Termux是一个Android终端模拟器,允许在手机上运行Linux环境,所以用户可能想用旧手机或平板作为控制中心,这样既经济又环保。 接下来,我需要确定搭建的基本步骤。首先,用户需要安装Termux,这个很简单,但可能需要指导他们如何正确安装和配置,比如更新源和安装必要工具如curl、git、python等。然后是安装必要的服务,比如MQTT代理,因为智能家居设备通常用MQTT协议通信。Mosquitto是一个常用的选择,但Termux官方仓库可能没有,所以需要从第三方安装,比如使用termux-services来管理服务。 然后,用户可能需要一个控制面板,Node-RED是一个低代码平台,适合可视化流程,应该是个不错的选择。需要指导他们安装Node.js,然后使用npm安装Node-RED,并设置开机启动。此外,Home Assistant也是一个流行的开源家居自动化平台,但可能在Termux上安装比较复杂,需要Python环境和相关依赖,这里可能需要详细步骤,或者建议使用Docker,但Termux的Docker支持有限,可能需要使用proot环境。 接着,需要考虑设备接入,比如ESP8266/ESP32等单片机,用户可能想通过MQTT或HTTP与Termux通信。需要给出示例代码,比如用Python写一个简单的MQTT客户端,或者用curl发送HTTP请求。同时,安全方面也不能忽视,比如设置MQTT的用户名密码,防火墙配置,SSH安全连接等。 用户可能遇到的常见问题包括服务无法启动,依赖缺失,或者网络配置问题。需要提前给出解决建议,比如检查termux-services是否正确安装,环境变量是否设置,或者端口是否被占用。此外,Termux在后台运行可能会被系统杀死,建议使用Termux:Boot或Tasker来保持服务常驻。 可能还需要提到一些扩展功能,比如使用Termux的传感器API获取手机本身的传感器数据,或者用摄像头做监控,但这对新手来说可能有点复杂。另外,外网访问可以通过ngrok或cloudflare隧道实现,方便远程控制。 最后,整理步骤时要清晰,分点说明,确保用户能一步步跟随。同时,提供参考链接和示例代码,帮助用户进一步学习。需要避免使用复杂术语,保持解释简单明了,特别是考虑到用户可能不是技术专家,所以步骤要详细,并指出可能的坑和解决方法。</think>在 Termux 中搭建智能家居控制中心,可以利用手机/平板作为低成本服务器,结合开源工具控制 IoT 设备。以下是分步指南: --- ### **1. 准备工作** - **安装 Termux** 从 [F-Droid](https://f-droid.org/packages/com.termux/) 下载最新版(避免使用 Play 商店旧版本)。 - **初始化环境** ```bash pkg update && pkg upgrade pkg install python nodejs git curl wget openssh ``` --- ### **2. 核心组件安装** #### **(1) MQTT 消息代理(Mosquitto)** ```bash pkg install mosquitto termux-setup-storage # 允许访问存储 mkdir -p ~/mosquitto/config echo "listener 1883" > ~/mosquitto/config/mosquitto.conf sv-enable mosquitto # 启动并设置开机自启 ``` #### **(2) Node-RED 可视化控制面板** ```bash npm install -g --unsafe-perm node-red sv-enable node-red # 启动服务 ``` - 访问 `http://localhost:1880` 设计控制流程(需配合 `ssh -L 1880:localhost:1880 your_phone_ip` 进行端口转发)。 --- ### **3. 设备接入示例** #### **(1) ESP32 通过 MQTT 通信** ```arduino #include <WiFi.h> #include <PubSubClient.h> const char* ssid = "你的WiFi"; const char* password = "密码"; const char* mqtt_server = "手机IP"; WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); WiFi.begin(ssid, password); client.setServer(mqtt_server, 1883); } void loop() { if (!client.connected()) reconnect(); client.publish("home/sensor", "温度:25℃"); delay(5000); } ``` #### **(2) Termux 端 Python 脚本订阅 MQTT** ```python import paho.mqtt.client as mqtt def on_message(client, userdata, msg): print(f"主题: {msg.topic}, 数据: {msg.payload.decode()}") client = mqtt.Client() client.connect("localhost", 1883) client.subscribe("home/#") client.on_message = on_message client.loop_forever() ``` --- ### **4. 进阶方案:Home Assistant** ```bash # 安装 Python 虚拟环境 python -m venv ha-env source ha-env/bin/activate pip install wheel homeassistant # 首次启动(会自动生成配置文件) hass ``` - 配置文件路径:`~/homeassistant/configuration.yaml` - 访问 `http://localhost:8123` 完成初始化,通过集成添加 MQTT、Zigbee 等设备。 --- ### **5. 安全与优化** - **防火墙配置** ```bash pkg install termux-firewall ufw allow ssh # 仅允许必要端口 ``` - **SSH 远程管理** ```bash pkg install openssh passwd # 设置密码 sshd # 启动服务 ``` - **后台保活** 安装 [Termux:Boot](https://f-droid.org/packages/com.termux.boot/),创建 `~/.termux/boot/` 目录并添加启动脚本。 --- ### **常见问题解决** - **端口占用**:使用 `ss -tulpn` 查看端口使用情况。 - **服务自启失败**:确认已安装 `termux-services` 并正确配置 `sv-enable`。 - **设备无法连接**:检查手机防火墙,确保 Termux 拥有后台运行权限。 --- 通过上述步骤,你可以将旧手机变身为智能家居中枢,支持灯光控制、温度监测等功能。扩展方向包括: - 集成 Telegram Bot 实现消息推送 - 使用 Termux API 调用手机传感器(如光照、加速度计) - 通过 Tasker 联动手机自动化任务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值