Auto launch Polybar at boot with default WM (GNOME, Mutter, 21.04)

I'm new to both polybar and Linux.

I have follow the tutorial on Github:

  1. Created a file called ~/.config/polybar/launch.sh

  2. Make it executable (I have confirm this through the GUI)

Here's the content of launch.sh:

#!/usr/bin/env bash

killall -q polybar

echo "---" | tee -a /tmp/polybar.log
polybar tonton 2>&1 | tee -a /tmp/polybar.log & disown
echo "Bars launched..."

Running this command from terminal works:

polybar tonton 2>&1 | tee -a /tmp/polybar.log & disown

that means I got my bar name correctly.

Running the launch script manually also works.

Am I missing something? I feel like I should tell the OS to run the launch script on startup somehow... I don't think it will recognise automatically.

I tried using crontab to run launch.sh automatically by adding this line to crontab file:

@reboot export DISPLAY=:0 && /home/atran/.config/polybar/launch.sh

but after checking the log file, this is what I got:

---
polybar|error: X connection error... (what: Socket, pipe or stream error)

What am I missing?

Edit: fix the crontab command by adding export display and now having a different error


Solution 1:

You have to run PolyBar after Xorg launch, and after login to desktop session.

The best way to do so - use XDG Autostart with below long command:

mkdir -p ~/.config/autostart/
cat <<EOF > ~/.config/autostart/polybar.desktop
[Desktop Entry]
Type=Application
Exec=/home/atran/.config/polybar/launch.sh
Hidden=false
X-MATE-Autostart-enabled=true
Name=Polybar
EOF

and then reboot.