Notify-send and/or zenity from cron job not working

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
kjacobs

Notify-send and/or zenity from cron job not working

Post by kjacobs »

OK....I have been banging my head on a wall for hours trying to figure out how to get notifications to pop-up on screen. I am doing a public computer build with Linux Mint 18 MATE and want to warn users that the computer will shutdown in x minutes. So here is a portion of my crontab.....

Code: Select all

45 18 * * 2 zenity --warning --text "This computer will shut down in 10 minutes." --display=:0.0
50 18 * * 2 zenity --warning --text "This computer will shut down in 5 minutes." --display=:0.0
53 18 * * 2 zenity --warning --text "This computer will shut down in 2 minutes. Please save your work or risk loss." --display=:0.0
55 18 * * 2 /sbin/shutdown -h now
The shutdown portion works fine but the warnings do not display on screen. Then I found that I had to add

Code: Select all

xhost local:YOUR_USERNAME > /dev/null
to my .bashrc and then the warnings would display. However.....there is a wrinkle. This public computer is setup to create a frozen guest session and then symlink to a hidden session to create the custom guest session.....as described here https://sites.google.com/site/easylinuxtipsproject/2. The cron job warnings only work for the user set in .bashrc. How can I get the warnings to display on the desktop regardless of who is logged in, especially for the guest account which gets deleted at each logout. I have also tried this with notify-send and had the same issues. I can use either notification option (zenity or notify-send).....I just need the simplest solution to display the warnings for all users.

Thanks
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Notify-send and/or zenity from cron job not working

Post by Pilosopong Tasyo »

Caveat: I'm using Ubuntu 16.04.x, but it should [hopefully] work in Mint 18.x.

I use the following code fragment as template when I need to display a message to any logged-in user:

sudo -u username DISPLAY=:number /path/to/notifier parameters ...

In order for this to work, you need two bits of information: the username and the corresponding display number used for that user. Using the who command will produce the required info:

Code: Select all

administrator@main-desktop:~/Desktop$ who
administrator tty7         2016-11-24 12:05 (:0)
guest-roehmi tty8         2016-11-24 12:20 (:1)
In the above example, I have two users running concurrently, me (administrator) in VT7 and a guest account in VT8. Filter out the extra info using tr and cut:

Code: Select all

administrator@main-desktop:~/Desktop$ !! | tr -d -s '()' ' '
who | tr -d -s '()' ' '
administrator tty7 2016-11-24 12:05 :0
guest-roehmi tty8 2016-11-24 12:20 :1

administrator@main-desktop:~/Desktop$ !! | cut -d ' ' -f 1,5
who | tr -d -s '()' ' ' | cut -d ' ' -f 1,5
administrator :0
guest-roehmi :1
tr removes the extra whitespace and the parentheses, leaving you 5 columns separated by a single space. You only need the first and fifth columns; cut will do that for you.

Now that you have the needed information, create a script that contains the necessary instructions to display a message. You could refer to the following pseudocode as template (I'll leave the actual coding to you as exercise):

/path/to/announce

Code: Select all

#! /bin/bash

MESSAGE=$*
USER_LIST="/tmp/user.list"

who command and filters here, results saved to $USER_LIST

repeat the following until the end of file $USER_LIST has been reached
  read USERNAME and DISPLAY_NUMBER from $USER_LIST
  sudo command here ... notification command here ... "$MESSAGE" &
end loop

optional - delete $USER_LIST
Pay attention to the & at the end of the zenity command. Unlike notify-send, zenity doesn't immediately return control to the calling script. It waits for you to click a button before it terminates. Running it as a background process ensures continued iteration of the script regardless if the user interacts with zenity or not.

Enable the script's execution bit, and edit root's cron table: sudo crontab -e

Code: Select all

45 18 * * 2 /path/to/announce "10 minute warning"
50 18 * * 2 /path/to/announce "5 minute warning"
53 18 * * 2 /path/to/announce "2 minute warning"
55 18 * * 2 /sbin/shutdown -h now
Done.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
kjacobs

Re: Notify-send and/or zenity from cron job not working

Post by kjacobs »

Sorry for the delayed reply......but WOW! Thanks for the detailed write-up and information. I ended up rebuilding the machine a slightly different way and the notifications work fine now. However, I will definitely keep this info in mind for any future builds.

Thanks again!
Locked

Return to “Scripts & Bash”