[deepin exploration] 针对近期搜狗+fcitx5出现卡住的解决方案
Tofloor
poster avatar
鲜衣怒马
deepin
2025-07-14 15:16
Author

搜狗+Fcitx5 高 CPU 监控解决方案

可解决近期出现的搜狗输入法莫名其妙的导致卡键盘卡输入法的问题。

📌 方案总览

方案 自动化 安装难度 适用场景
🔧 基础方案 ❌ 手动 ⭐ 极简 临时应急
🚀 高级方案 ✅ 全自动 ⭐⭐⭐ 中等 长期监控(技术用户)
📦 DEB包 ✅ 全自动 ⭐ 极简 一键安装(普通用户)

🔧 方案一:基础方案(手动执行)

✅ 优点

  • 无需配置,即开即用
  • 轻量级(仅1行命令)

❌ 缺点

  • 需手动执行,无法自动处理

📜 脚本内容

#!/bin/bash
kill -9 $(ps -ef | grep "/usr/bin/fcitx5 -r" | grep -v grep | awk '{print $2}')

💻 使用步骤

  1. 保存为 kill_fcitx5.sh
  2. 添加执行权限:
    chmod +x kill_fcitx5.sh
    
  3. 卡顿时运行:
    ./kill_fcitx5.sh
    

🚀 方案二:高级方案(自动监控)

✅ 优点

  • 5秒检测间隔
  • CPU>5%自动杀死进程
  • 资源限制(内存50M/CPU20%)

❌ 缺点

  • 需要编辑配置文件
  • 卸载需手动清理

🛠️ 实施步骤

1. 创建监控脚本

sudo nano /usr/local/bin/fcitx5-monitor.sh
#!/bin/bash
THRESHOLD=5       # CPU阈值(%)
INTERVAL=5        # 检测间隔(秒)

while true; do
    PROCESS_INFO=$(/bin/ps -eo pid,%cpu,comm | /usr/bin/awk -v th="$THRESHOLD" '/fcitx5$/ && !/awk/ {cpu=int($2); if(cpu>th) print $1, cpu}')

    if [ -n "$PROCESS_INFO" ]; then
        read PID CPU_USAGE <<< "$PROCESS_INFO"
        /usr/bin/logger -t fcitx5-monitor "检测到高CPU: PID=$PID, CPU=${CPU_USAGE}% > ${THRESHOLD}%,正在杀死..."
  
        if /bin/kill -9 "$PID" 2>/dev/null; then
            /usr/bin/logger -t fcitx5-monitor "成功杀死fcitx5 (PID=$PID)"
        else
            /usr/bin/logger -t fcitx5-monitor "杀死进程失败 (PID=$PID)"
        fi
    fi

    /bin/sleep "$INTERVAL"
done

2. 设置权限

sudo chmod +x /usr/local/bin/fcitx5-monitor.sh
sudo chown root:root /usr/local/bin/fcitx5-monitor.sh

3. 创建Systemd服务

sudo nano /etc/systemd/system/fcitx5-monitor.service
[Unit]
Description=Fcitx5 High CPU Monitor
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/fcitx5-monitor.sh
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=fcitx5-monitor
MemoryMax=50M
CPUQuota=20%

[Install]
WantedBy=multi-user.target

4. 启动服务

sudo systemctl daemon-reload
sudo systemctl enable --now fcitx5-monitor.service

5. 验证状态

systemctl status fcitx5-monitor.service
journalctl -u fcitx5-monitor -f  # 实时日志

📦 方案三:一键安装(DEB包)

✅ 优点

  • 双击安装
  • 自动配置服务
  • 无需命令行

❌ 缺点

  • 卸载需手动操作

📂 DEB包结构



fcitx5-monitor-deb/
├── usr/
│   └── local/
│       └── bin/
│           └── fcitx5-monitor.sh
├── etc/
│   └── systemd/
│       └── system/
│           └── fcitx5-monitor.service
└── DEBIAN/
    ├── control
    └── postinst
    ├── prerm         # 卸载前执行

🎯 最终建议

用户类型 推荐方案
普通用户 📦 DEB包一键安装
技术用户 🚀 高级方案
临时解决 🔧 基础方案

🎯 版本历史

版本 日期 更新内容
1.0.3 2025-07-15 新增卸载时清理,kill成功后等待5分钟,等待下一个进程自动拉起后稳定后,再开始监测
1.0.0 2025-07-10 初始发布,实现基础监控功能

立即体验无卡顿的输入法环境! 🚀
20250715-20:49 更新,如果有之前安装了的请更新版本,太灵敏了,会在fcitx5起来的瞬间又吧进程杀死了,

image.png
下面的1.0.3版本是可以正常的版本
fcitx5-monitor-deb-v1.0.3.zip

双击打开压缩文件;再双击deb文件安装;

  1. 双击安装(或命令行):
    sudo dpkg -i fcitx5-monitor.deb
    
  2. 验证安装:
    systemctl status fcitx5-monitor.service
    

效果图
(安装后服务状态示例)

常见问题

1. 如何调整CPU阈值?

sudo nano /usr/local/bin/fcitx5-monitor.sh
# 修改THRESHOLD值(默认5)
THRESHOLD=10

2. 如何卸载服务?

sudo dpkg -r fcitx5-monitor #会清除包括脚本、service文件、日志文件在内的所有应用数据

3. 如何查看日志?

journalctl -u fcitx5-monitor -n 50  # 最近50条
Reply Favorite View the author
All Replies
鲜衣怒马
deepin
2025-07-14 15:19
#1

正常启动运行,效果如何后续再更
image.png

Reply View the author
deepin-superuser
deepin
2025-07-14 15:20
#2

纯 fcitx5 卡还是有搜狗输入法的时候卡?

Reply View the author
鲜衣怒马
deepin
2025-07-14 15:23
#3
deepin-superuser

纯 fcitx5 卡还是有搜狗输入法的时候卡?

有搜狗的时候卡涩,但是我看解决方案,只要重启fcitx5 就行,不用重启电脑

Reply View the author
青山断处是泷州
deepin
2025-07-14 15:55
#4

为啥我用搜狗没有出现卡顿呢?是不是电脑硬件跟不上系统的配置要求,经过测试deepin系统对于类Debian系统来说资源占有太高,无论是硬件资源还是软件资源

Reply View the author
鲜衣怒马
deepin
2025-07-14 16:13
#5
青山断处是泷州

为啥我用搜狗没有出现卡顿呢?是不是电脑硬件跟不上系统的配置要求,经过测试deepin系统对于类Debian系统来说资源占有太高,无论是硬件资源还是软件资源

触发条件不清楚,我时i7 -1260P

Reply View the author
deepin账号
deepin
2025-07-14 16:25
#6

靠强制重启根本没解决问题

Reply View the author
deepin账号
deepin
2025-07-14 16:26
#7

把im-config删掉。然后直接去配置环境变量!

https://fcitx-im.org/wiki/Setup_Fcitx_5/zh-cn

Reply View the author
鲜衣怒马
deepin
2025-07-14 16:32
#8
deepin账号

靠强制重启根本没解决问题

临时解决~applaud

Reply View the author
鲜衣怒马
deepin
2025-07-14 16:53
#9
deepin账号

把im-config删掉。然后直接去配置环境变量!

https://fcitx-im.org/wiki/Setup_Fcitx_5/zh-cn

对用户要求太高,环境变量这东西,不太敢改

Reply View the author
鲜衣怒马
deepin
2025-07-14 16:56
#10
It has been deleted!
MeGusta
deepin
2025-07-14 17:13
#11

是不是可以创建当前用户,而不是root用户的systemd服务,这样就不需要sudo权限,也不需要在/usr下写入文件。

Reply View the author
鲜衣怒马
deepin
2025-07-14 17:27
#12
MeGusta

是不是可以创建当前用户,而不是root用户的systemd服务,这样就不需要sudo权限,也不需要在/usr下写入文件。

应该是可以的,但是我用nobady 用户时,启动时报用户错误,没去解决就直接用的root

Reply View the author
鲜衣怒马
deepin
2025-07-15 09:22
#13

1752483292526.png

更新一下,在检测到CPU高时:
这时可能已经卡了2-5s,开始卡之后要2-5s CPU 占用才会升起来,所以此时监测到了CPU 高,会自动杀掉进程恢复;
这个方案只是临时解决,并不能彻底解决不卡住的问题,可以在卡住时快速恢复,勉强能用吧!
不用手动处理,解决一些痛点,至于更彻底的解决就交给深度和搜狗吧

Reply View the author
鲜衣怒马
deepin
21 hours ago
#14

如果有安装了1.0.1版本的请更新1.0.3版本,不好意思,1.0.1版本有一点问题,会导致fcitx5 CPU高被杀之后,自动拉起来又被杀

1.0.3版本更新了逻辑,如果kill 成功,就等待5分钟之后再继续监控CPU

Reply View the author
卢少骅
deepin
11 hours ago
#15

like 很实用

Reply View the author
鲜衣怒马
deepin
2 hours ago
#16
卢少骅

like 很实用

谢谢,我也是用了一段时间手动后,突然想起搞个服务算了

Reply View the author
Μαησεωφ
deepin
an hour ago
#17

上策:放弃搜狗,拥抱万象拼音。

Reply View the author
鲜衣怒马
deepin
35 minutes ago
#18
Μαησεωφ

上策:放弃搜狗,拥抱万象拼音。

确实,后面试试这个

Reply View the author