Photo by Unsplash
535 字
3 分钟
MTProxy脚本无自启动配置问题排查
MTProxy 脚本无自启动配置问题排查
装了个 ellermister/mtproxy 的一键脚本,发现每次重启服务器都要手动运行。查了一下,这个脚本本身根本没有写开机自启动的逻辑。
问题分析
脚本提供了 start、daemon、stop 等命令,但没有:
- 写入
/etc/systemd/system/(Systemd 服务) - 写入
/etc/init.d/(SysVinit 服务) - 添加 crontab 的
@reboot任务
它就是个”绿色”脚本——运行时在后台起进程,但不告诉系统”下次开机记得叫我”。
解决方案一:Systemd(推荐)
用 Systemd 管理最规范,有日志、有自动重启。
1. 创建服务文件
nano /etc/systemd/system/mtproxy.service写入内容(注意修改路径):
[Unit]Description=MTProxy ServiceAfter=network.target
[Service]Type=simple# 必须填对,脚本依赖当前目录找配置WorkingDirectory=/root/mtproxyExecStart=/bin/bash /root/mtproxy/mtp.sh daemon
# 崩溃自动重启Restart=alwaysRestartSec=5
[Install]WantedBy=multi-user.target关键点
WorkingDirectory必须填对,因为脚本里有WORKDIR=$(dirname $(readlink -f $0)),依赖当前目录来找config和bin/mtg。
2. 启用并启动
# 重载配置systemctl daemon-reload
# 设置开机自启systemctl enable mtproxy
# 停止之前手动运行的(防止端口冲突)bash /root/mtproxy/mtp.sh stop
# 启动系统服务systemctl start mtproxy
# 查看状态systemctl status mtproxy看到 active (running) 就是成功了。
解决方案二:Crontab(简单但不推荐)
如果嫌 Systemd 麻烦,用 crontab 也能实现开机启动,但缺点是没有日志管理和崩溃重启:
# 编辑 crontabcrontab -e
# 添加一行@reboot /bin/bash /root/mtproxy/mtp.sh start脚本命令速查
# 启动bash mtp.sh start
# 守护模式(自带循环重启)bash mtp.sh daemon
# 调试运行(前台输出)bash mtp.sh debug
# 停止bash mtp.sh stop
# 重启bash mtp.sh restart
# 重新安装bash mtp.sh reinstall查看连接信息
脚本安装完后运行 bash mtp.sh 会显示:
TMProxy+TLS代理: 运行中服务器IP:xxx.xxx.xxx.xxx服务器端口:443MTProxy Secret: xxxxxxxxxxxxxxTG一键链接: tg://proxy?server=xxx&port=443&secret=xxxx多版本支持
脚本支持三种实现:
- MTProxy (官方版,仅 x86_64,问题较多)
- mtg (Golang 版,兼容性强,推荐)
- mtprotoproxy (Python 版,兼容性强)
安装时选 2 或 3,1 官方版坑比较多。
参考链接
- MTProxyTLS 脚本:https://github.com/ellermister/mtproxy
- Telegram MTProto 代理文档:https://core.telegram.org/mtproto
- Systemd 服务文档:https://systemd.io/
MTProxy脚本无自启动配置问题排查
https://im.awsl.app/posts/networking/035-mtproxy-autostart/