Ubuntu – use cron to chime at top of hour like a grandfather clock

cronscriptssound

I get lost in askubuntu reading Q&A and experimenting with recommended code snippets. So I often miss switching TV news channels on the internet at the top of the hour. Not a big problem for youtube channels that allow time-shifting, but it's a "show stopper" for tv channels using flash player.

Can cron be configured to run a job at the top of each hour where I provide a script to sound a grandfather like clock chime (with subtle sound volume of course!)? A minor chime could be sounded at the bottom of the hour too. I'm sure I can locate suitable .ogg or .oga files on the net and I know I can use ogg123 to play them. I just don't know if cron could do it. I know I could use a script that sleeps and wakes up to test time but don't like the thought of having something running all the time (there's enough of that going on already I think).

If this can be done with cron can someone answer how to set it up?

Thank you.

Best Answer

heemayl has provided the correct answer to get cron to run at the top of the hour and bottom of the hour. Thanks again! However as other users have discovered a cronjob cannot play sound files by default:


Configuring cron to play sounds

In order for cron to play sound files it needs to export an environment variable:

export XDG_RUNTIME_DIR="/run/user/1000"

Once this is done in your script sounds will play.

As pointed out by another answer your user ID may not always be 1000. To find your user ID use:

$ id

uid=1000(rick) gid=1000(rick) groups=1000(rick),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)

If your user ID is not 1000, then change the script to your ID.

Configure cron to run top of hour and bottom of hour

crontab -e setup (last 5 lines only):

# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
00 *    *   *   *    /usr/local/bin/top-of-hour-chime    >/dev/null
00,30 * *   *   *    /usr/local/bin/bottom-of-hour-chime >/dev/null

Most users setup cron to use the nano editor. Use these control keys in nano to save your work:

  • Add the last two lines above and use Ctrl+O to write Out (save) the file.
  • A /tmp... filename is presented, but do not be concerned, simply press Enter.
  • Now use Ctrl+X to eXit the nano editor.

The bottom-of-hour-chime script runs at the top of the hour too but, that is OK since it's short and sweet.

If you don't have the >/dev/null at the end of the lines cron tries to email you with the command output. If you don't have the mail server setup an error message appears in your /var/log/syslog file:

Oct 02 10:00:07 dell CRON[21259]: (CRON) info (No MTA installed, discarding output)

Configuring cron to display pop-up notifications

The script displays a pop-up notification message at the top of every hour. So for this an additional variable needs to be exported:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";

Contents of top-of-hour-chime

#! /bin/bash

# NAME: top-of-hour-chime
# PATH: /usr/local/bin
# DESC: Play sound called by cron
# CALL: Automatically called by /var/spool/cron/crontabs/<user_name>
# DATE: Created Oct 1, 2016. Modified June 10, 2018.
# UPDT: 2016-11-09 - Add notification bubble with time in text.
#       2018-06-10 - Switch from `ogg123` to `paplay` in default installation.

# exit # uncomment to suppress when watching movies

# paplay needs environment variable exported:
export XDG_RUNTIME_DIR="/run/user/1000"

# needed for notify-send
eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";

# get hour in 12 hour format
HOUR=$(date +%I)
ZHOUR=$(echo $HOUR | sed 's/^0*//')

pactl set-sink-volume 0 -25%
DISPLAY=:0 notify-send --urgency=critical --icon=/usr/share/icons/gnome/256x256/status/appointment-soon.png "It is ""$ZHOUR"" o'clock"
paplay '/usr/share/sounds/ubuntu/ringtones/Melody piano.ogg'
pactl set-sink-volume 0 +25%

The export XDG_RUNTIME_DIR... is necessary to get sound to play. This is because cron runs in minimal environment. This line should be omitted first if it you use a third party sound player. Then if no sound put the line back. Then still if no sound, well happy hunting!

The two main TV news shows I watch over the net have softer volume than normal so I have the TV turned up higher than normal. Consequentially when ogg123 plays sound files they sound very loud. So pactl is used to reduce volume before chime and increase it afterwards.

Contents of bottom-of-hour-chime

This is almost a duplicate of top-of-hour-chime and perhaps redundant here, but included for full documentation purposes.

#! /bin/bash

# NAME: bottom-of-hour-chime
# PATH: /usr/local/bin
# DESC: Play sound called by cron
# CALL: Automatically called by /var/spool/cron/crontabs/<user_name>
# DATE: Oct 1, 2016. Modified June 10, 2018.

# UPDT: 2018-06-10 Switch from `ogg123` to `paplay` installed by default.

# exit # Uncomment to suppress when watching movies.

# paplay needs environment variable exported:
export XDG_RUNTIME_DIR="/run/user/1000"

pactl set-sink-volume 0 -25%
paplay /usr/share/sounds/ubuntu/ringtones/Bliss.ogg
pactl set-sink-volume 0 +25%
Related Question