Skip to content

Instantly share code, notes, and snippets.

@damoahdominic
Created January 25, 2026 13:22
Show Gist options
  • Select an option

  • Save damoahdominic/31b53ad98ec2f1e6b0fc0b6426e04c92 to your computer and use it in GitHub Desktop.

Select an option

Save damoahdominic/31b53ad98ec2f1e6b0fc0b6426e04c92 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Persist XDG_RUNTIME_DIR and enable systemd user services
# Add to .profile (works for all POSIX shells)
if ! grep -q "XDG_RUNTIME_DIR" ~/.profile 2>/dev/null; then
echo 'export XDG_RUNTIME_DIR=/run/user/$(id -u)' >> ~/.profile
echo "✓ Added XDG_RUNTIME_DIR to ~/.profile"
else
echo "✓ XDG_RUNTIME_DIR already in ~/.profile"
fi
# Add to .bashrc if it exists
if [ -f ~/.bashrc ]; then
if ! grep -q "XDG_RUNTIME_DIR" ~/.bashrc; then
echo 'export XDG_RUNTIME_DIR=/run/user/$(id -u)' >> ~/.bashrc
echo "✓ Added XDG_RUNTIME_DIR to ~/.bashrc"
else
echo "✓ XDG_RUNTIME_DIR already in ~/.bashrc"
fi
fi
# Add to .zshrc if it exists
if [ -f ~/.zshrc ]; then
if ! grep -q "XDG_RUNTIME_DIR" ~/.zshrc; then
echo 'export XDG_RUNTIME_DIR=/run/user/$(id -u)' >> ~/.zshrc
echo "✓ Added XDG_RUNTIME_DIR to ~/.zshrc"
else
echo "✓ XDG_RUNTIME_DIR already in ~/.zshrc"
fi
fi
# Enable systemd user session lingering
sudo loginctl enable-linger $USER
echo "✓ Enabled systemd user lingering"
# Apply changes to current session
export XDG_RUNTIME_DIR=/run/user/$(id -u)
echo "✓ Applied to current session"
# Verify
echo ""
echo "Current XDG_RUNTIME_DIR: $XDG_RUNTIME_DIR"
echo ""
echo "Testing systemctl --user:"
systemctl --user status >/dev/null 2>&1 && echo "✓ systemctl --user is working!" || echo "✗ systemctl --user still has issues"
echo ""
echo "Done! Changes will persist across logins."
echo "If systemctl still doesn't work, you may need to log out and back in."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment