Compton是Ubuntu中常用的轻量级窗口合成器,用于提升窗口透明度、阴影等视觉效果。要让其在系统启动时自动运行,可通过以下5种主流方法实现,覆盖传统与现代场景:
systemd是Ubuntu 20.04及以上版本的默认初始化系统,通过创建自定义服务单元文件实现Compton开机自启。
your-username替换为你的实际用户名):sudo nano /etc/systemd/system/compton.service
ExecStart参数,如添加--backend glx开启GPU加速):[Unit]
Description=Compton Window Compositor
After=graphical.target  # 确保图形环境就绪后启动
[Service]
Type=simple
User=your-username  # 以普通用户身份运行(避免权限问题)
ExecStart=/usr/bin/compton --config ~/.compton.conf  # 指定Compton路径及配置文件
Restart=on-failure  # 崩溃后自动重启
[Install]
WantedBy=multi-user.target  # 多用户模式(图形界面)启动
sudo systemctl daemon-reload  # 重新加载systemd配置
sudo systemctl enable compton.service  # 设置开机自启
sudo systemctl start compton.service  # 立即启动(可选,验证配置)
sudo systemctl status compton.service
若显示“active (running)”,则表示设置成功。Ubuntu的“启动应用程序”工具可直观添加自启动项,无需修改配置文件。
gnome-session-properties命令),点击“添加”。compton --config ~/.compton.conf(若配置文件在默认路径~/.compton.conf,可简化为compton)若系统支持rc.local(Ubuntu 18.04默认启用),可通过编辑该文件实现自启动。
sudo nano /etc/rc.local
exit 0之前)添加以下内容(&表示后台运行,避免阻塞启动流程):/usr/bin/compton --config ~/.compton.conf &
sudo chmod +x /etc/rc.local
重启系统后,Compton将随系统启动。通过crontab的@reboot指令,可实现用户级Compton自启动(无需root权限)。
crontab -e
--backend glx为可选的GPU加速参数):@reboot /usr/bin/compton --config ~/.compton.conf --backend glx
Ctrl+X→Y→Enter保存,重启系统后Compton将自动启动。若系统使用SysV Init(如Ubuntu 16.04及更早版本),可通过update-rc.d命令设置自启动。
/etc/init.d/目录下创建脚本(如compton):sudo nano /etc/init.d/compton
your-username为实际用户名):#!/bin/bash
### BEGIN INIT INFO
# Provides:          compton
# Required-Start:    $graphical.target
# Required-Stop:     $null
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Compton at boot
# Description:       Lightweight window compositor for Ubuntu
### END INIT INFO
case "$1" in
  start)
    su - your-username -c "/usr/bin/compton --config ~/.compton.conf &"
    ;;
  stop)
    pkill compton
    ;;
  *)
    echo "Usage: /etc/init.d/compton {start|stop}"
    exit 1
    ;;
esac
exit 0
sudo chmod +x /etc/init.d/compton  # 赋予执行权限
sudo update-rc.d compton defaults  # 注册服务(设置开机自启)
重启系统后,Compton将自动启动。~/.compton.conf存在(若不存在,可通过compton --config /dev/null生成默认配置)。root),防止权限冲突。shadow false)或降低刷新率(vsync false),提升启动速度。通过以上方法,均可实现Compton在Ubuntu中的自动启动,选择适合你系统版本和操作习惯的方式即可。