温馨提示×

怎样写linux compton脚本

小樊
45
2026-10-06 17:31:48
栏目: 智能运维

在 Linux 上写 Compton(或已演变为 picom) 脚本,通常用于开机自启、热重载配置、不同场景切换配置等。下面从基础脚本、自启脚本、管理脚本、示例配置一步步说明。


一、先确认你用的是 compton 还是 picom

很多发行版已用 picom(compton 的 fork):

which compton
which picom
  • 有 picom → 用 picom
  • 只有 compton → 用 compton

下面以 picom 为例(compton 完全类似)


二、最基础的启动脚本

1️⃣ 简单启动脚本 start-compton.sh

#!/bin/bash

# 杀掉已有进程
pkill -x picom

# 启动 picom
picom --config ~/.config/picom/picom.conf -b

赋予执行权限:

chmod +x start-compton.sh

三、带日志的脚本(推荐)

方便排查问题:

#!/bin/bash

pkill -x picom

picom \
  --config ~/.config/picom/picom.conf \
  --log-level info \
  --log-file ~/.cache/picom.log \
  -b

四、多配置切换脚本(很常用)

例如:

  • 日常:模糊 + 阴影
  • 游戏:无特效,性能优先

目录结构

~/.config/picom/
 ├── picom.conf
 ├── perf.conf
 └── blur.conf

切换脚本 compton-switch.sh

#!/bin/bash

case "$1" in
  blur)
    pkill -x picom
    picom --config ~/.config/picom/blur.conf -b
    ;;
  perf)
    pkill -x picom
    picom --config ~/.config/picom/perf.conf -b
    ;;
  *)
    echo "Usage: $0 {blur|perf}"
    ;;
esac

使用:

./compton-switch.sh blur
./compton-switch.sh perf

五、开机自启(不同桌面环境)

✅ i3 / bspwm / sway

在 ~/.config/i3/config 或 ~/.xinitrc:

exec ~/.local/bin/start-compton.sh

或:

picom --config ~/.config/picom/picom.conf -b

✅ systemd 用户服务(推荐)

~/.config/systemd/user/picom.service

[Unit]
Description=picom compositor
After=graphical-session.target

[Service]
ExecStart=/usr/bin/picom --config %h/.config/picom/picom.conf
Restart=on-failure

[Install]
WantedBy=default.target

启用:

systemctl --user enable --now picom

六、热重载配置(不重启)

picom 支持 SIGUSR1 重载:

pkill -USR1 picom

可写成脚本:

#!/bin/bash
pkill -USR1 picom

七、示例 picom 配置(常用)

~/.config/picom/picom.conf

backend = "glx";
vsync = true;

shadow = true;
shadow-radius = 12;
shadow-opacity = 0.4;

blur:
{
  method = "gaussian";
  size = 10;
};

fading = true;
fade-in-step = 0.03;
fade-out-step = 0.03;

unredir-if-possible = true;

八、调试脚本(排错)

#!/bin/bash

pkill -x picom
picom --config ~/.config/picom/picom.conf --diagnostics

或直接前台运行看输出:

picom --config ~/.config/picom/picom.conf

九、总结一句话

Compton 脚本 = 杀进程 + 指定配置 + 后台运行

如果你愿意,可以告诉我:

  • 你用的是 哪个发行版 / 桌面环境
  • 想要 性能优先还是美观优先

我可以直接帮你写一份可用的完整脚本 + 配置。

0 踩