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.