Ubuntu – Ubuntu 14.04 and playing songs from cron

alsacronenvironment-variablespulseaudiosound

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

Best Answer

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

Related Question