As I understand it, cron | crontab
runs in it's own environment; e.g., see accepted answer in:
https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user
Thus, even though aplay
will play a sound (WAV
) file when invoked as a regular user or as root (su
| sudo
), it won't play that file when it is called from cron
-- e.g., executing a bash (.sh) script that contains your aplay
statement).
This works, on my Arch Linux system.
Here is my cron_notification.sh
script:
#!/usr/bin/bash
# /mnt/Vancouver/Programming/scripts/cron_notification.sh
# For use with crontab [ sudo gedit /etc/crontab ]
# cron (Arch Linux: cronie) requires full paths:
# /usr/bin/aplay
# /usr/bin/notify-send
for i in 1 2 3 4 5
do
#aplay alarm.mp3 ## << aplay cannot play MP3 files; use WAV
# ----------------------------------------
# NEEDED TO RUN 'aplay' FROM crontab:
# https://unix.stackexchange.com/questions/231941/cant-run-aplay-as-root
# https://www.reddit.com/r/linuxquestions/comments/37vcbo/playing_audio_from_a_cronjob/
# PulseAudio needs XDG_RUNTIME_DIR, so:
XDG_RUNTIME_DIR=/run/user/`id -u` /usr/bin/aplay /mnt/Vancouver/Programming/scripts/PHASER.WAV
# ----------------------------------------
sleep 0.25
done
# ----------------------------------------------------------------------------
# "Critical" alerts persist until clicked (i.e., do not appear, then fade after ~20"):
# notify-send -u critical 'Hello Victoria!' 'Countdown has ended!' --icon=dialog-information
/usr/bin/notify-send -u critical 'Hello Victoria!' "It's 3 pm!" -i /mnt/Vancouver/Programming/scripts/alert.jpg
And, here is the relevant portion of my /etc/crontab
file:
# /etc/crontab
# system-wide crontab
# edit: sudo {your favorite text editor: gedit; geany; ...} /etc/crontab
# https://crontab.guru ## online crontab values checker, planner
# =====================================================================
# SHELL
# =====================================================================
# cron (crontab) requires full paths:
SHELL=/usr/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# =====================================================================
# ARCH LINUX-RELATED [Arch Linux x86_64]
# =====================================================================
# cron | https://wiki.archlinux.org/index.php/Cron
# Will install, use "cronie" cron (crontab):
# Install cronie: sudo pacman -Syu cronie
# Enable cron: sudo systemctl enable --now cronie.service
# Start cron: sudo systemctl start cronie.service
# Restart cron: sudo systemctl restart cronie.service
# =====================================================================
# APLAY NOTIFICATION (3:00pm M-F)
# =====================================================================
# Two issues when running
# /mnt/Vancouver/Programming/scripts/cron_notification.sh
# from cron:
# ----------------------------------------
# 1. "notify-send":
# https://bbs.archlinux.org/viewtopic.php?id=216912
# notify-send also needs access to your DBUS_SESSION_BUS_ADDRESS. Assuming that your UID is 1000:
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
# More here: https://wiki.archlinux.org/index.php/Desktop_notifications#Usage_in_programming
# ----------------------------------------
# 2. "aplay":
# TO RUN 'aplay' FROM crontab:
# https://www.reddit.com/r/linuxquestions/comments/37vcbo/playing_audio_from_a_cronjob/
# PulseAudio needs XDG_RUNTIME_DIR, so:
# XDG_RUNTIME_DIR=/run/user/`id -u` /usr/bin/aplay /mnt/Vancouver/Programming/scripts/PHASER.WAV
# Line above added to "/mnt/Vancouver/Programming/scripts/cron_notification.sh" script.
# ----------------------------------------
# m h dom mon dow user nice command
# "At 15:00 on every day-of-week from Monday through Friday”
# [https://crontab.guru/#0_15_*_*_1-5]:
0 15 * * 1-5 victoria nice -n 19 /usr/bin/bash /mnt/Vancouver/Programming/scripts/cron_notification.sh
# NOTE -- running as user ("victoria"), not "root".
# Test - every minute:
#* * * * 1-5 victoria nice -n 19 /usr/bin/bash /mnt/Vancouver/Programming/scripts/cron_notification.sh
I left the comments in place, for references and clarity.


You can download the PHASER.WAV
sound file from my website, here:
http://persagen.com/files/PHASER.WAV