5

I recently upgraded my computer to Ubuntu 14.04.

After the upgrade the system no longer plays the few songs I had it play via crontab.

The crontab lines look like so:

0 9   *   *   1-5       /usr/bin/mpg123 -q /home/user/Music/song.mp3

I get no error from cron upon running the command. If I run the command manually via SSH the song plays successfully.

I prepended the command in crontab with 'env' to get a better grip of the environment under which cron runs the job:

0 9   *   *   1-5       env; /usr/bin/mpg123 /home/user/Music/song.mp3

This generated the following output:

HOME=/home/user
LOGNAME=user
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
PWD=/home/user
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
    version 1.16.0; written and copyright by Michael Hipp and others
    free software (LGPL) without any warranty but with best wishes

Directory: /home/user/Music/
Playing MPEG stream 1 of 1: song.mp3 ...

MPEG 1.0 layer III, 320 kbit/s, 44100 Hz stereo
Comment:  00000000 00000210 00000AA0 000000000058C5D0 00000000 00506FD7 00000000 00000000 00000000 00000000 00000000 00000000

[2:11] Decoding of song.mp3 finished

To make the interactive shell of my ssh connection more like the one cron is running under I unset all but the specified environment vaiables:

for a in `env | awk -F= '{ print $1 }' | grep -v "HOME\|LOGNAME\|PATH\|LANG\|SHELL\|PWD" | xargs`; do unset $a; done

And after that change copy+paste from crontab no longer produce any sound. The program seem to play the file however:

$ /usr/bin/mpg123 /home/user/Music/song.mp3
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
        version 1.16.0; written and copyright by Michael Hipp and others
        free software (LGPL) without any warranty but with best wishes

Directory: /home/user/Music/
Playing MPEG stream 1 of 1: song.mp3 ...

MPEG 1.0 layer III, 128 kbit/s, 44100 Hz joint-stereo

[2:51] Decoding of song.mp3 finished.

Leading me to believe that the sound is just output to the wrong device.

Switching to aplay the followning produces no sound:

$ aplay /usr/share/sounds/alsa/Front_Center.wav
Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono

But specifying this device does play over HDMI as intended:

$ aplay -D plughw:CARD=NVidia,DEV=3 /usr/share/sounds/alsa/Front_Center.wav
Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono

How can I set that device as the system default? (Which it seems to be whenever I have a richer environment.)

Alternatively, how can I force mgp123 to use the correct device? Does the ALSA-device plughw:CARD=NVidia,DEV=3 correlate to a device in /dev?

mpg123 will let you specify a particular device like so:

   -a dev, --audiodevice dev
          Specify the audio device to use.  The default is system-dependent (usually /dev/audio or /dev/dsp).  Use this option if  you  have  multiple  audio
          devices and the default is not what you want.

The plughw devices in alsa:

$ aplay -L | grep plughw -A2
plughw:CARD=NVidia,DEV=0
    HDA NVidia, ALC889A Analog
    Hardware device with all software conversions
plughw:CARD=NVidia,DEV=1
    HDA NVidia, ALC889A Digital
    Hardware device with all software conversions
plughw:CARD=NVidia,DEV=3
    HDA NVidia, HDMI 0
    Hardware device with all software conversions

Seem to match up with the pcmC0D?p-devices in /dev/snd:

$ ls /dev/snd/ | egrep [013]p
pcmC0D0p
pcmC0D1p
pcmC0D3p

But using it does not seem to help mpg123:

$ /usr/bin/mpg123 -q -a /dev/snd/pcmC0D3p /home/user/Music/song.mp3
[oss.c:129] error: Can't reset audio!
[mpg123.c:690] error: ...in decoding next frame: Unable to set up output format! (code 1)

I don't really understand what ALSA and/or PulseAudio is in respect to this.

Neither seem to work in the limited environment:

$ pactl list cards
Connection failure: Connection refused
pa_context_connect() failed: Connection refused
$ alsamixer
Error opening terminal: unknown.

Both commands work with "normal" environment variables set.

I downloaded and ran alsa-info.sh, first with "normal" environment:

http://www.alsa-project.org/db/?f=e2be8ceb9ef289ff9ef95557396dd82b88f337e0

When I tried to run it with most environment variable unset I got an error.

After setting:

export TERM=xterm

I got the script running again. Result here:

http://www.alsa-project.org/db/?f=67fdd80bd55c76cd6dd30c981bf02fe66ba3d9d6

EDIT:

Further investigation of environment variable unsetting shows that I only have to unset XDG_RUNTIME_DIR to break playback.

$ unset XDG_RUNTIME_DIR

EDIT2:

If I abandon mgp123 for cvlc I get sound when playing via crontab like so:

 0 9  *   *    1-5      /usr/bin/cvlc -q --alsa-audio-device="hw:0,3" --play-and-exit /home/user/Music/song.mp3 2> /dev/null
  • 1
    - From aplay -L , the working device when you test with aplay is HDMI audio output (CARD=NVidia,DEV=3 HDA NVidia, HDMI 0) : do you really use HDMI display audio output and not normal jack output ? For alsamixer error, do you use ssh connection or local terminal ? Can you run "Alsa-info" to show your audio config (someone can have an idea) : copy the following command in a terminal , choose "upload" option when asked ("tab" key to select it) an post url printed at the end. wget http://www.alsa-project.org/alsa-info.sh -O alsa-info.sh && bash alsa-info.sh – laugeo Sep 29 '14 at 19:58
  • Yes, it is my intention to play sound over HDMI. I'm connecting to the machine using SSH. – azzid Sep 30 '14 at 8:23
  • I've run and uploaded the output of alsa-info.sh. Links in post. – azzid Sep 30 '14 at 8:39
1

At As you use ssh connection , try to add your user in the "audio" group :
sudo usermod -aG audio username
reboot and test that alsamixer is ok , so you can set volumes: Probably you have to press F6 to select the 3rd device.

Then,in my case, I can read sound connected by ssh on a remote pc without X server using:

  • cvlc ( which comes with vlc-nox package):
    cvlc --alsa-audio-device="hw:0,3" /usr/share/sounds/alsa/Front_Center.wav
    If not ok, change hw:0,3 to hw:0,0 or hw:0,1
    In my case, it works also via crontab.
  • mpg123: this works also launched via ssh:
    mpg123 -a hw:0,3 Musique/test.mp3

Note that in my case, pulseaudio is disable on the remote pc (not sure disabling is useful as pulseaudio is started thru X and lightdm session ). To verify that pulseaudio is off and not auto respawning, run various times killall -9 pulseaudio

  • user is already in audio group. changing to cvlc works, but seems like overkill? – azzid Sep 30 '14 at 13:57
  • 1
    it depends what format you want to play,but i edited the answers: works also with mpg123 – laugeo Oct 1 '14 at 7:37
7

So, I just set the environment variable XDG_RUNTIME_DIR the same as in my working shells, and it works. I found this page because I had this same issue with trying to get sound to play from cron jobs -- mpg123 seeming to think it plays the file fine, but with no audio. Setting that variable fixed it.

  • This worked for me, thanks! The cron installed on my system was happy to let me just dump XDG_RUNTIME_DIR=/run/user/<my $UID> on a line in crontab (this won't work everywhere unfortunately) – Mark McDonald May 15 at 7:01
1

Despite using this export:

# paplay needs environment variable exported:
export XDG_RUNTIME_DIR="/run/user/1000"

it didn't work.

After months of research found no solution.

In desperation, tried the following and it worked:

Enter the following in contab

# Terminal command: crontab -e
# Detect the name of the display in use
display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
user=$(who | grep '('$display')' | awk '{print $1}') # the user of the display 
uid=$(id -u $user) #Detect the id of the user

XDG_RUNTIME_DIR=/run/user/${uid}

*/5 * * * * sh /root/.play_sound.sh

Next create .play_sound.sh

/root/.play_sound.sh

display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
user=$(who | grep '('$display')' | awk '{print $1}') # the user of the display 
uid=$(id -u $user) #Detect the id of the user

su - $user -c "paplay /usr/share/sounds/freedesktop/stereo/complete.oga"

Hope this helps.

  • Been years since I left that system behind, so I can’t really verify your findings, but your write up looks good. Thanks for keeping the topic alive. – azzid Oct 25 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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