Skip to content

Instantly share code, notes, and snippets.

@andrewjong
Created November 27, 2020 16:18
Show Gist options
  • Save andrewjong/28e766a7cf5df75c4f38d2c7f4444c95 to your computer and use it in GitHub Desktop.
Save andrewjong/28e766a7cf5df75c4f38d2c7f4444c95 to your computer and use it in GitHub Desktop.
Allows "notify-send" to be run by cron to show up for the user.
#!/bin/sh
# Executes a program as the currently logged in user. e.g. in /etc/crontab:
# */5 22 * * * root cron-prog notify-send "Closing computer now!"
[ "$#" -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 | head -1 )
export DBUS_SESSION_BUS_ADDRESS=$(cat /proc/"$env_reference_process"/environ | grep --null-data ^DBUS_SESSION_BUS_ADDRESS= | sed 's/DBUS_SESSION_BUS_ADDRESS=//')
export DISPLAY=$(cat /proc/"$env_reference_process"/environ | grep --null-data ^DISPLAY= | sed 's/DISPLAY=//')
"$program" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment