535 字
3 分钟
MTProxy脚本无自启动配置问题排查

MTProxy 脚本无自启动配置问题排查#

装了个 ellermister/mtproxy 的一键脚本,发现每次重启服务器都要手动运行。查了一下,这个脚本本身根本没有写开机自启动的逻辑

问题分析#

脚本提供了 startdaemonstop 等命令,但没有:

  • 写入 /etc/systemd/system/(Systemd 服务)
  • 写入 /etc/init.d/(SysVinit 服务)
  • 添加 crontab 的 @reboot 任务

它就是个”绿色”脚本——运行时在后台起进程,但不告诉系统”下次开机记得叫我”。

解决方案一:Systemd(推荐)#

用 Systemd 管理最规范,有日志、有自动重启。

1. 创建服务文件#

Terminal window
nano /etc/systemd/system/mtproxy.service

写入内容(注意修改路径):

/etc/systemd/system/mtproxy.service
[Unit]
Description=MTProxy Service
After=network.target
[Service]
Type=simple
# 必须填对,脚本依赖当前目录找配置
WorkingDirectory=/root/mtproxy
ExecStart=/bin/bash /root/mtproxy/mtp.sh daemon
# 崩溃自动重启
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
关键点

WorkingDirectory 必须填对,因为脚本里有 WORKDIR=$(dirname $(readlink -f $0)),依赖当前目录来找 configbin/mtg

2. 启用并启动#

Terminal window
# 重载配置
systemctl daemon-reload
# 设置开机自启
systemctl enable mtproxy
# 停止之前手动运行的(防止端口冲突)
bash /root/mtproxy/mtp.sh stop
# 启动系统服务
systemctl start mtproxy
# 查看状态
systemctl status mtproxy

看到 active (running) 就是成功了。

解决方案二:Crontab(简单但不推荐)#

如果嫌 Systemd 麻烦,用 crontab 也能实现开机启动,但缺点是没有日志管理和崩溃重启:

Terminal window
# 编辑 crontab
crontab -e
# 添加一行
@reboot /bin/bash /root/mtproxy/mtp.sh start

脚本命令速查#

Terminal window
# 启动
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
服务器端口:443
MTProxy Secret: xxxxxxxxxxxxxx
TG一键链接: tg://proxy?server=xxx&port=443&secret=xxxx

多版本支持#

脚本支持三种实现:

  1. MTProxy (官方版,仅 x86_64,问题较多)
  2. mtg (Golang 版,兼容性强,推荐)
  3. mtprotoproxy (Python 版,兼容性强)

安装时选 2 或 3,1 官方版坑比较多。

参考链接#

MTProxy脚本无自启动配置问题排查
https://im.awsl.app/posts/networking/035-mtproxy-autostart/
作者
uu
发布于
2025-11-20
许可协议
CC0 1.0