anhsirk21
Posts: 6
Joined: Wed Nov 27, 2019 1:43 am

Cron sechduling to play music using mpg123 issue

Sun Aug 01, 2021 7:54 am

mpg123 having an issue with playing music at the scheduled time.

Current code on crontab -

Code: Select all

00 07 * * * /usr/bin/mpg123 --shuffle /home/pi/Documents/Cron-task/music/*.mp3
cron shows up on syslog but nothing is played over the speakers.
music plays fine over the speakers but does not play with scheduled crontab.

thanks

GlowInTheDark
Posts: 1958
Joined: Sat Nov 09, 2019 12:14 pm

Re: Cron sechduling to play music using mpg123 issue

Sun Aug 01, 2021 8:46 am

As with all things cron, you need to get logging working in order to figure out what is wrong.

The short summary is: There's something different in the environment running in cron than when you run it manually.

Anyway, the following link may be interesting reading for you:

https://blog.devgenius.io/set-an-alarm- ... a6554d5e49
Poster of inconvenient truths.

Linux zealot and proud of it.

"nokcid" wants to date me.

hal8000
Posts: 265
Joined: Fri Oct 04, 2013 5:22 pm

Re: Cron sechduling to play music using mpg123 issue

Sun Aug 01, 2021 9:06 am

As normal user, open a terminal and test your command with

Code: Select all

/usr/bin/mpg123 --shuffle /home/pi/Documents/Cron-task/music/*.mp3
If that command does not work, then there is some error in either your path or spelling (everything is case sensitive)
As crontab is run as root and root user has access to /bin/bin/ you can enter your crontab as:

Code: Select all

   00 07 * * * mpg123 --shuffle /home/pi/Documents/Cron-task/music/*.mp3

pcmanbob
Posts: 11660
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Cron sechduling to play music using mpg123 issue

Sun Aug 01, 2021 9:40 am

hal8000 wrote:
Sun Aug 01, 2021 9:06 am

As crontab is run as root and root user has access to /bin/bin/ you can enter your crontab as:
Cron can be run as root using " sudo crontab -e "

or as pi user using " crontab -e "

It looks like the OP is running it as pi user.


anhsirk21

Try changing your cron line to this

Code: Select all

00 07 * * * mpg123 --shuffle /home/pi/Documents/Cron-task/music/*.mp3 >> /home/pi/log.txt 2>&1
then after the time it should have run look in the file log.txt and see if there are any errors.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

GlowInTheDark
Posts: 1958
Joined: Sat Nov 09, 2019 12:14 pm

Re: Cron sechduling to play music using mpg123 issue

Sun Aug 01, 2021 9:51 am

If that command does not work, then there is some error in either your path or spelling (everything is case sensitive)
User has already stipulated that it works as expected when run interactively.
As crontab is run as root and root user ,,,
crontab(1) is (almost) never run as root. You run it as the user (i.e., user pi in most cases).

cron jobs themselves are run as the user from whose crontab files they are from. (Again, user pi in most cases).
Poster of inconvenient truths.

Linux zealot and proud of it.

"nokcid" wants to date me.

User avatar
thagrol
Posts: 5558
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Cron sechduling to play music using mpg123 issue

Sun Aug 01, 2021 6:51 pm

  • Which OS?
  • Booting to desktop or command line?
  • When it works interactively, was that via a local login or over ssh/vnc?
  • To the desktop or command line?
If you're running RPiOS with desktop it might be pulse audio related. AIUI, pulse only starts on login and is a per user/session process. Could be a lack of a pulse daemon or a permissions problem but with no log/error message it's impossible to say.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

anhsirk21
Posts: 6
Joined: Wed Nov 27, 2019 1:43 am

Re: Cron sechduling to play music using mpg123 issue

Mon Aug 02, 2021 5:46 am

hal8000 wrote:
Sun Aug 01, 2021 9:06 am
As normal user, open a terminal and test your command with

Code: Select all

/usr/bin/mpg123 --shuffle /home/pi/Documents/Cron-task/music/*.mp3
If that command does not work, then there is some error in either your path or spelling (everything is case sensitive)
As crontab is run as root and root user has access to /bin/bin/ you can enter your crontab as:

Code: Select all

   00 07 * * * mpg123 --shuffle /home/pi/Documents/Cron-task/music/*.mp3
I am able to play music through my terminal but not with crontab.
would using sudo crontab -e make a difference?

W. H. Heydt
Posts: 14973
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Cron sechduling to play music using mpg123 issue

Mon Aug 02, 2021 5:59 am

anhsirk21 wrote:
Sun Aug 01, 2021 7:54 am
mpg123 having an issue with playing music at the scheduled time.

Current code on crontab -

Code: Select all

00 07 * * * /usr/bin/mpg123 --shuffle /home/pi/Documents/Cron-task/music/*.mp3
cron shows up on syslog but nothing is played over the speakers.
music plays fine over the speakers but does not play with scheduled crontab.

thanks
About all I can say is that here is how I did something similar...
Crontab entries (the first line plays an existing track, the second line streams from our local Classical station):

Code: Select all

45 06 * * 7 mplayer -volume 60 /media/pi/MUSIC/messiah/track_32.wav
58 06 * * 7 /home/pi/bin/runkdfc
Runkdfc also displays the stream metadata in a lxterminal window on the 7' RPF display:

Code: Select all

#!/bin/bash

PATH=/home/pi/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
export DISPLAY=:0.0

lxterminal --geometry=70x7 -e mplayer -cache 512 -volume 60 -playlist http://playerservices.streamtheworld.com/pls/KDFCFMAAC.pls
The above works. The selected lines are from my "alarm clock". Perhaps you can use what I did as a guide to get your own system working.

User avatar
scruss
Posts: 4299
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Cron scheduling to play music using mpg123 issue

Mon Aug 02, 2021 8:57 am

anhsirk21 wrote:
Mon Aug 02, 2021 5:46 am
I am able to play music through my terminal but not with crontab.
would using sudo crontab -e make a difference?
No, you don't need to use sudo. This should make it work:

Code: Select all

00 07 * * * DISPLAY=:0.0 /usr/bin/mpg123 --shuffle /home/pi/Documents/Cron-task/music/*.mp3
For various reasons, sound playing is typically attached to where the display (X server) is running on Linux. The cron process doesn't know about any display settings for the system, so can't play sounds unless you tell it how.

(This is one of the old and confusing bits about ‘the Unix way’ that I wish would go away forever. How much sense does it make to have to do something about a display when you want sound? None!)
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Return to “General discussion”