paplay not working from cron
A little more than a year ago I wrote a notifier app. (on Ubuntu 16.04.7) which played some audio with paplay and then put up a yad window with the text of the notification. This could be run either from command line or from cron. Recently I installed Ubuntu 20.04.3 and found that running from command line still works but running from cron doesn't. I have a bin directory below my home directory where the script (remind.sh) is kept. The pared down version of remind.sh is the following. Note that when I run from cron I get no audio indication the script has run but the trace.txt file gets updated and yad produces a window.
#!/bin/bash
# reminder script called from either
# 1. cron or
# 2. directly from shell to pop-up or
# 3. gidday.sh
# produces a 'yad' window with a picture and message in it.
/usr/bin/paplay /home/gary/sounds/marimba.ogg
# trace the run
d=`date`
echo "done -- "$d >> trace.txt
# now put out the window
#
#/usr/bin/yad --borders=50 --scroll --image=/home/gary/Pictures/shrunk-pictures/${ar[$t]} --title="${ar[$t]}" --text-align=center --mouse --width=800 --height=400 --text='<span font="20">'"$txt"'</span>' 2>/dev/null
Sorry, I should have provided the following in my original post. The line in crontab that fires off the remind1.sh script is:
36 13 30 aug * export DISPLAY=:0 && export MESSG="get movies from lib" && /home/gary/bin/remind1.sh
As I said before, this all ran perfectly fine in Ubuntu 16.04. And it still doesn't explain why the paplay command doesn't run but the (uncommented) yad command does.
Solution 1:
Jobs run through cron
, or systemd
startup scripts aren't run in the same runtime environment that you have on your desktop. systemd
startup scripts are run as root
. None of your PATH
changes, or other environment variable settings from your ~/.bashrc
are automatically propagated to your cron
job. For example, there's no $DISPLAY
, so GUI programs need special treatment (read man xhost
).
One can set environment variables for all one's cron
jobs in the crontab
file
Read man 5 crontab
.
Look at the results of echo "=== id ===";id;echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias
in each of your environments.
Since the command
part of the crontab
line is, by default, interpreted by /bin/sh
, which has a simpler syntax than /bin/bash
, I recommend having command
be a call to a bash
script (executable, mounted, starts with #!/bin/bash
) which sets up the environment, then calls the desired program.