Two sound files at the same time - problem with crontab

Hi altogether, :wave:

I am curious as how to solve a peculiar “problem”. Nothing major but I´m still interested in a possible solution. :wink:

I have Big-Ben chimes played out every quarter of an hour and put the respective entries in my crontab.

Example:

15 * * * * if [ ! -f /tmp/Ruhe ]; then mplayer /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/Big_Ben_quarter_hour > /dev/null 2>&1; fi

That works very well. :+1:
But I run into problems if I have some other noise played out at the same time. :neutral_face:

Background:

I can´t stand complete silence and thus need some sort of background noise on a permanent basis.

As a rule I run this command:

firejail mpv --loop=inf /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Musik/sounds_von_anoise/sounds/coffee_shop.ogg
from within a terminal.

which plays some coffee-shop background noise in a permanent loop.
This works well too. :+1:

However: when running the mpv-command for background noise I don´t get the chimes any more…
… which is weird as they ARE played back when issuing the respective command from within a terminal. :roll_eyes:

So, the command

firejail mplayer /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/Big_Ben_quarter_hour

works even if “coffee-shop” is running at the same time!
But it doesn´t work whenever it´s started by crontab!

BTW: the fact that I run the command within the sandbox “firejail” is of no consequence whatsoever.
The result is the same when issuing the command outside the sandbox.

Does anybody have a clue why that “problem” exists :question:

Many thanks in advance.

Greetings
Rosika :slightly_smiling_face:

I rarely need sound on Linux systems, so I cannot comment from experience regarding this specific topic, however what I have plenty of experience with is crontab. One of the most important things I have learned about crontab is:

Crontab is weird.

It often does unexpected things and is way stupider than almost every user expects, when first starting to use crontab.
Here are some of the stones this tool lays into your way, when you are trying to properly use it:

  • It does not know your environment, i.e. it does not have any PATH by default. Usually people solve this by only using absolute paths in their scripts or by sourcing the desired PATH in some other way, before letting the actual business commands run.
  • Each user has their own crontab. Sometimes, you need to put something specifically into root's crontab, other times it’s enough to put it inside your own.

There are other caveats I have forgotton about at this moment, but which I would encounter next time I would need to deal with cron.

I doubt the above problems are applicable to your specific situation, however it perhaps may already help you to analyse the situation anew, with in a different way. Just do not expect cron to know anything about your script or the applications you run. Always keep in mind how stupid cron is and that you need to tell it everything by hand, or else it perhaps won’t work properly. If you look at it again the way described, then you could maybe find an error you were missing before.

Another thing you could do is classic error checking, so the task can tell you what is wrong. Remove the silencing from your command and then look up the contents of the cron.log, so you can see what error the task is encountering when trying to run.

1 Like

Hi @Akito :wave:

thank you so much for your very detailed reply. :smiley:

Thanks for sharing your views on this.

I quite agree the described phenomenon should be attributed to crontab.
As my system has no problems playing two sound files at the same time when issued from the command-line it´s only logical to assume crontab would be the culprit. :wink:

Curious thing though.
I cannot find cron.log on my system.
locate cron.log doesn´t return anything.
Or can it be that the log-file is only created if anything goes wrong with cron? :question:

Well, as I said earlier the whole thing doesn´t pose much of a problem. I was just curious to find out a bit more about it. :blush:

Thanks so much, Akito.

Many greetings.
Rosika :slightly_smiling_face:

Apparently, it depends on the system:

1 Like

Thank you @Akito for clarifying the matter of cron.log and thanks for the link.

On a default installation the cron jobs get logged to

/var/log/syslog

You can see just cron jobs in that logfile by running

 grep CRON /var/log/syslog

That´s the case with my system. :wink:

I deliberately toggled the silencing command so that the chimes should be played at 3:45 PM (coffe-house noise still playing).
The entry I found was this:

May 12 15:45:01 rosika-10159 CRON[19499]: (rosika) CMD (if [ ! -f /tmp/Ruhe ]; then mplayer /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/Big_Ben_three_quarters_of_the_hour > /dev/null 2>&1; fi)

which seems to indicate that the command was executed. Still I could only hear coffe-house. :neutral_face:

BTW: As I have lnav installed I can see the same message here.

Thanks a lot for helping, Akito.

Many greetings.
Rosika :slightly_smiling_face:

Which means you should ideally change the task to something like this:

if [ ! -f /tmp/Ruhe ]; then mplayer /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/Big_Ben_three_quarters_of_the_hour >/tmp/chimes.log 2>&1; fi

Follow the log:

tail -f /tmp/chimes.log
1 Like

Hi @Akito,

thanks so much for the suggestion. That I can do. :blush:

In the meantime I may add a bit of additional info as I tried out the following scenario:
At 4:30 PM:

  • NO coffee-house file was running, i.e. I was waiting for the chimes to kick in as defined per cronjob

  • chimes indeed began playing

  • chimes still playing: I started coffee-house file, which couldn´t be played immediately but started after chimes had finished.

So I looked at the logs and found this:

 May 12 16:30:01 rosika-10159 CRON[23951]: (rosika) CMD (if [ ! -f /tmp/Ruhe ]; then mplayer /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/Big_Ben_half_hour > /dev/null 2>&1; fi)
 May 12 16:30:18 rosika-10159 pulseaudio[1323]: Error opening PCM device front:0: Das Gerät oder die Ressource ist belegt    # device or resource busy/occupied                                                                                   

The problem thus appears to be pulseaudio-related… :roll_eyes:

Thanks a lot, Akito.
Many greetings.
Rosika :slightly_smiling_face:

@Akito:

Hi again.
Picking up your latest suggestion I altered the crontab-entry to

[...]>/tmp/chimes.log 2>&1 [...]

and took at look at the log-file:

Here´s what it came up with:

tail: /tmp/chimes.log: Datei abgeschnitten
MPlayer 1.3.0 (Debian), built with gcc-9 (C) 2000-2016 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
Terminal type `unknown' is not defined.

Playing /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/Big_Ben_three_quarters_of_the_hour.
libavformat version 58.29.100 (external)
Audio only file format detected.
Load subtitles in /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/
==========================================================================
Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
AUDIO: 44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)
Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
==========================================================================
AO: [pulse] Init failed: Connection refused
Failed to initialize audio driver 'pulse'
[AO_ALSA] alsa-lib: pcm_hw.c:1715:(snd_pcm_hw_open) open '/dev/snd/pcmC0D0p' failed (-16): Device or resource busy
[AO_ALSA] alsa-lib: pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
[AO_ALSA] Playback open error: Device or resource busy
Failed to initialize audio driver 'alsa'
[AO SDL] Samplerate: 44100Hz Channels: Stereo Format s16le
[AO SDL] using aalib audio driver.
[AO SDL] Unable to open audio: No available audio device
Failed to initialize audio driver 'sdl:aalib'
Could not open/initialize audio device -> no sound.
Audio: no sound
Video: no video


Exiting... (End of file)

It seems that pulse is involved here too. :roll_eyes:
(“Failed to initialize audio driver ‘pulse’”)

Many greetings.
Rosika :slightly_smiling_face:

Sorry in advance @Rosika - going to hijack your thread to have a rant about cron…

I like cron when it’s used in the “UNIX” way - i.e. user cronjobs (if they’re allowed in /etc/cron.allow or not explicitly barred by /etc/cron.deny) go in /var/spool/cron/crontabs (I think it’s /var/spool/cron/cronjobs in Solaris) - which they can edit in place, but preferably done using “crontab -e” command (because when you save it tells crond to re-read scheduled jobs).

But then you find some Linux server where developers or application admins have plonked cronjobs in a plethora of folders like /etc/cron.daily/ or /etc/cron.weekly/ blah blah blah - and you can’t find their cronjob they want you to fix or troubleshoot for them…

I don’t ever remember stuff like /etc/cron.{hourly,daily,weekly}/ from my UNIX / Solaris days - why did Linux part ways with this simplicity?

Anyone know if there’s a way of querying EVERY possible location for some command/sring that’s in some cron entry in those folders? I guess it could be done with a for loop for “find … -exec” command - but it needs a consistent method - and I’ve yet to find one…

2 Likes

My experience is, that Linux often makes things easier compared to its predecessors, sacrificing a bit of human backward compatability in the process.

One of the most famous examples is the “what is one block” discussion.

Another example of how Linux tries at least a bit to be more human than its predecessors is this:
https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Bash-POSIX-Mode

Basically, if you want to run Bash in POSIX mode, you make everything harder and stupider. But if you run it in its normal mode, i.e. pretty much the Linux mode, everything makes more sense and you even get additional features.

What I always found funny about POSIX is that it tries to make a standard for operating systems to follow, to ensure cross-compatability, however if your standard is so fucked and elitistic, how can you expect most operating systems to actually implement it as requested. In my opinion, POSIX is another example of how the elitist mindset is living in its own dimension, disregarding a lot of sense for intuitivity and human behaviour out of idealistic reasons.

As far as I know UNIX already was POSIX compliant and BSD, etc. are for sure POSIX compliant. I do not see the appeal in that, though.

1 Like

I’m wondering if there is a way to have say coffee-house playing in a different Pulse-Audio channel? As much as I like Pulse-Audio it still relies when music or any sound is played, to play it through it’s default channel. The above link might point you in the right direction?
Here is How To Get The Best Out Of PulseAudio

1 Like

Hi all,

sorry I couldn´t answer earlier but I wasn´t online yesterday. :blush:

@daniel.m.tripp:

No need to apologize at all. I like feedback of any kind. Thanks for your post, Dan. :slightly_smiling_face:

@Akito:
Thanks to you as well for the additional info. :slightly_smiling_face:

1 Like

@clatterfordslim:
Hi Mark,

thanks a lot for the links you provided. I´ll look into them.

That´s an idea. Hopefully there´s a way for me to achieve that. :blush:

What strikes me as odd is the fact that playing the two files (coffee-house with mpv and the Big-Ben strikes with mplayer at the same time from the terminal doesn´t pose a problem at all.
It´s only when the BIg-Ben strikes are triggered by cron (coffee-house already playing) that they don´t seem to be played…

Many thanks for your suggestions.

Greetings.
Rosika :slightly_smiling_face:

Hi all, :wave:

in the meantime I tried to get some help form the folks at MPlayer-users mailing-list.

I thought I´d share some of their findings here, in case anyone is interested. :wink:

Alexander Strasser wrote:

You can’t easily just share one sound hardware with many users. So
there is software on top that allows you to do this. E.g pulse audio
is one such solution.

[…] I infer, that when all the processes use pulse audio
for output it works for you. When your mplayer is started by cron,
it can’t connect to pulse audio for some reason. Then it tries to use
alsa which fails because it can’t get the audio device for itself.

and Roger Heflin wrote:

To run audio from another session (ie cron or an ssh login) you need to set
all sorts of stuff for pulseaudio to work even if it is the same user.
Cron has none of that environment set. There may be something else in
addition to the environment, or it may simply be just environment vars.
[…]
Without that it does not know where the sound goes, nor where the video
goes if something has video.

You might be better off making a shell script to do the same thing and
starting it up from the interactive session.

So it seems things are a bit more complex than I expected them to be. :blush:

Well, I´ll try investigating a bit further but I doubt I will be successful in the end.

Many greetings.
Rosika :slightly_smiling_face:

Pretty much what I said previously: cron is a very rudimentary tool.

1 Like

Thanks @Akito for the links you provided.

They pointed me in the right direction. :+1:

Taking a closer look at cron - Help using crontab to play a sound - Ask Ubuntu I found the instructions there to be quite useful.

To test the new method I created a script versuch.sh and made it executable:

#!/bin/bash

export XDG_RUNTIME_DIR="/run/user/1000"
mplayer /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/Big_Ben_quarter_hour

Then I entered the new line

11 * * * * /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Pausenzeichen_Skripte/versuch.sh > /dev/null 2>&1

in my crontab.

And indeed it worked! :+1: :heart_eyes:

Although the coffee-shop-loop was already playing the Big-Ben-chimes were run (and audible) through the script. Both audio-files were played back at the same time. :smile:

After the test I had a look at ksystemlog which showed me those entries:

17.05.21 15:11	CRON	pam_unix(cron:session): session opened for user rosika by (uid=0)

17.05.21 15:11	CRON	(rosika) CMD (/media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Pausenzeichen_Skripte/versuch.sh > /dev/null 2>&1)

17.05.21 15:11	CRON	pam_unix(cron:session): session closed for user rosika

I think the output seems fine.

It´s actually the

export XDG_RUNTIME_DIR="/run/user/1000"

part of the script which did the trick.
I found a reference to that on cron - Help using crontab to play a sound - Ask Ubuntu .

Here it was actually a recommendation stating this should work:

#!/bin/bash
export XDG_RUNTIME_DIR="/run/user/1000"
mplayer -really-quiet "$1" -volume 100

I am not sure what the “$1” entry is supposed to do, so I left it out in my script which seems to work alright all the same.

So I can transfer my cron-entries to scripts now and reference them from crontab.

Thanks Akito and all of you for your great help. It´s so much appreciated. :heart:

Many greetings.
Rosika :slightly_smiling_face:

1 Like
#!/bin/bash

echo "===="
echo "$1" "$2" "$3"

echo
echo

echo "===="
echo "$@"

Save this to whatsthis.sh.

Then run:

bash whatsthis.sh Linux is Love
1 Like

Hi @Akito,

thanks for the explanation. :+1:

I got

====
Linux is Love


====
Linux is Love

as output.

I see “$@” yields each parameter as a separate word.
“$1”, “$2”, “$3” being the individual parameters.

O.K., that´s clear but i fail to understand why “$1” should be necessary in the script

#!/bin/bash
export XDG_RUNTIME_DIR="/run/user/1000"
mplayer -really-quiet "$1" -volume 100

Oh, I think I understand now. Surely “$1” references the audio file to be played…

Thanks a lot for helping, Akito.

Many greetings from Rosika :slightly_smiling_face:

1 Like

Hi once more,

I just found out there´s actually no need for the workaround with scripts to be referenced by crontab. There seems to be a simpler way.

On Cron › Wiki › ubuntuusers.de
I found an example for providing environment variables to the command like this:

* * * * * DISPLAY=:0 LANG=de_DE.UTF-8 zenity --info --text "Beispiel für das Starten eines Programms mit GUI"

So I thought why not do the same for solving my problem.
To test it I created the following entry crontab-entry:

42 * * * * if [ ! -f /tmp/Ruhe ]; then XDG_RUNTIME_DIR="/run/user/1000" mplayer /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/Big_Ben_quarter_hour > /dev/null 2>&1; fi

And once again it worked as desired. :relaxed:

ksystemlog showed the respective entries again:

17.05.21 16:42	CRON	pam_unix(cron:session): session opened for user rosika by (uid=0)

17.05.21 16:42	CRON	(rosika) CMD (if [ ! -f /tmp/Ruhe ]; then XDG_RUNTIME_DIR="/run/user/1000" mplayer /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Pausenzeichen/Big_Ben_chimes/Big_Ben_quarter_hour  > /dev/null 2>&1; fi)

17.05.21 16:42	CRON	pam_unix(cron:session): session closed for user rosika

So I basically have to prepend the mplayer command thus:
XDG_RUNTIME_DIR="/run/user/1000"

That´s even easier than creating a bunch of dedicated scripts. :smiley:

Many thanks once again.

Greetings from Rosika :slightly_smiling_face:

1 Like