0

I have been at this for a month now. New to programming. Trying to run a schedule for wav files to execute. Using Raspberry Pi 3 and Raspbian Stretch. My cron.d file called 'BellCron' is as follows:

/5 * * * * pi /home/pi/CronJobs/BellsCron.sh

Applied chmod +x to the BellsCron.sh file.

It is a file that executes a second file, 'playAngelus.py'. When I execute 'BellsCron.sh' at the command line it works fine.

The 'BellsCron.sh file looks like this:

#!/usr/bin/env python

sudo -u pi python /home/pi/CronJobs/playAngelus.py

The 'playAngelus.py' file looks like this:

#!/user/bin/env python

import pygame

import time

pygame.init()

pygame.mixer.music.load("/home/pi/Music/Angelus.wav")

pygame.mixer.music.play()

time.sleep(10)

I have also run a text file from the same cron.d file that is giving me the time in a file every 2 minutes, so I know it is running cron. But, the sound file will not work using cron. I accept that the error is mine. I have tried every configuration I can find on the web. I would be most appreciative if someone would help me to write the script I need to execute a wav file in my home directory, /home/pi/Music/Angelus.wav, using cron.d or crontab.

1
  • You shouldn't ever need to sudo -u pi …, as that's just reverting your cron job back to the default Raspbian user.
    – scruss
    Nov 15, 2017 at 23:29

4 Answers 4

0

In

sudo -u pi python /home/pi/CronJobs/playAngelus.py

I think 'python' should be replaced with full path of python executable, something like: /usr/bin/python

So your code should look something like: sudo -u pi /usr/bin/python /home/pi/CronJobs/playAngelus.py

Hope this solves your problem.

1
  • 1
    I was sooo close. After what seemed like 50 combinations of entries, you showed me the 51st, and the right one at that . Thank you for your help.
    – fjco
    Nov 14, 2017 at 18:59
1

Why not simply play the wav file directly from the cron job ?

*/5 * * * * /usr/bin/aplay /home/pi/Music/Angelus.wav
3
  • Tried this, but without success, even after reboot. But the solution was found. Thanks for your help.
    – fjco
    Nov 14, 2017 at 18:56
  • This works on my Pi3
    – CoderMike
    Nov 14, 2017 at 19:01
  • I'd be interested to see what your log states : 'grep CRON /var/log/syslog'
    – CoderMike
    Nov 14, 2017 at 20:45
0

If you're running a graphical login, audio depends on X display access. cron jobs don't have the right environment variables set. I had a setup that produced an hourly chime, and this is what worked.

Install play, the sox audio player:

sudo apt install sox libsox-fmt-all

Edit your crontab the recommended way:

crontab -e

Add this line, save and exit:

*/5 * * * * export DISPLAY=:0 && /usr/bin/play -q /home/pi/Music/Angelus.wav
1
  • Tried this, but to no avail. However, the solution was found. Thanks for your help.
    – fjco
    Nov 14, 2017 at 18:55
0

You can try to define XDG. For example:

* * * * * XDG_RUNTIME_DIR=/run/user/$(id -u) /path/to/script

Sound started working for me. Do not forget to edit job timing so it does not run every minute.

source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.