Skip to content

Instantly share code, notes, and snippets.

@swiesend
Created June 21, 2017 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swiesend/da0ac40de13f73cec0aefa65eaf0056d to your computer and use it in GitHub Desktop.
Save swiesend/da0ac40de13f73cec0aefa65eaf0056d to your computer and use it in GitHub Desktop.
get DBUS_SESSION_BUS_ADDRESS and DISPLAY for cronjobs with gui tasks
#!/bin/sh
[ "$#" -lt 1 ] && echo "Usage: $0 program options" && exit 1
program="$1"
shift
user=$(whoami)
env_reference_process=$( pgrep -u "$user" xfce4-session || pgrep -u "$user" ciannamon-session || pgrep -u "$user" gnome-session || pgrep -u "$user" gnome-shell || pgrep -u "$user" kdeinit )
export DBUS_SESSION_BUS_ADDRESS=$(cat /proc/"$env_reference_process"/environ | grep -z ^DBUS_SESSION_BUS_ADDRESS= | sed 's/DBUS_SESSION_BUS_ADDRESS=//')
export DISPLAY=$(cat /proc/"$env_reference_process"/environ | grep -z ^DISPLAY= | sed 's/DISPLAY=//')
"$program" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment