Skip to content

Instantly share code, notes, and snippets.

@alsolovyev
Last active August 29, 2020 11:25
Show Gist options
  • Save alsolovyev/d600e206d3b7f20f33f031723e4134cb to your computer and use it in GitHub Desktop.
Save alsolovyev/d600e206d3b7f20f33f031723e4134cb to your computer and use it in GitHub Desktop.
Launch any GUI related application from crontab in Ubuntu 16.04, 17.10 and other Ubuntu distributions with Gnome

Launch any GUI related application from crontab

#!/bin/bash -e
# Name: gui-launcher
# Description: Launch any GUI related application from crontab
# in Ubuntu 16.04, 17.10 and other Ubuntu distributions with Gnome
# Example: */1 * * * * /full/path/to/gui-launcher.sh "/full/path/to/custom/script.py"
# Link: https://askubuntu.com/questions/978382/how-can-i-show-notify-send-messages-triggered-by-crontab
# Warning: Please don't modify and run the script as root. It could be harmful for the system!
# Check whether the user is logged-in
while [ -z "$(pgrep gnome-session -n -U $UID)" ]; do sleep 3; done
# Export the current desktop session environment variables
export $(xargs -0 -a "/proc/$(pgrep gnome-session -n -U $UID)/environ")
# Execute the input command
nohup "$@" >/dev/null 2>&1 &
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment