'su - username -c command' not working as expected (the command is mplayer)

I have the following script:

/home/username/bin/play:

#!/bin/sh
/usr/bin/nice -n 20 /usr/bin/nohup > /dev/null /usr/bin/nice -n -20 /usr/bin/mplayer -cache 30000000 -loop 0 /home/username/audio.mp3 &

Running it when I am logged in as username works fine (the audio plays). But when I am logged in as root and I try:

su - username -c /home/username/bin/play

there is no sound (although there is mplayer process in ‘ps axf’). Same happens also if I do:

su - username
/home/username/bin/play

Why is it that there is no sound in the second scenario and how can I make it play?

Without doing a deeper investigation, your command will likely work if you use sudo instead of su.

TSU

Tried that too after reading your post. Same effect - no sound.

I don’t really know the answer. But I think it has to do with sound devices. They are “owned” – assigned to the user logged in at the console or main GUI session.

As far as I know, it has always been thus. The sound doesn’t work for other users. Or, at least, I have never succeeded with that.

I am just trying to run the bash script as username at system startup. I have put another bash script in rc3.d which has the line with “su - …” but it won’t play. It seems to me not a good idea to run mplayer as root because I also want to be able to stop it later as a username. So I hope someone who knows to explain.

nrickert is correct, I believe. Also, does every user have the audio.mp3 file in their /home/* subdirectory?
Lastly, why would you want to run mplayer as root? Is it somehow critical for your setup? If that’s the case, we might be able to work out a different solution :).

Leaving the (IMHO most) important question (“Why?”) aside, firstly there is this:

-cache 30000000

erm


-cache <kBytes>
              This option specifies how much memory (in **kBytes**) to use when precaching a file or URL.
              Especially useful on slow media.

30 Gigabytes of cache for an audio file? Interesting.

And secondly a short look into the crystal ball.

  • pulseaudio running

  • and running as $NORMAL_USER?

  • and blocking audio device for all other users (including root)?

whoami
axel

pulseaudio --start

su - c "mplayer -ao pulse /usr/share/sounds/alsa/test.wav"
support. You will not be able to use your remote control.

Playing /usr/share/sounds/alsa/test.wav.
libavformat version 57.56.101 (external)
Audio only file format detected.
Load subtitles in /usr/share/sounds/alsa/
==========================================================================
Trying to force audio codec driver family libmad...
Opening audio decoder: [pcm] Uncompressed PCM audio decoder
AUDIO: 44100 Hz, 2 ch, s16le, 1411.2 kbit/100.00% (ratio: 176400->176400)
Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)
==========================================================================
AO: [pulse] Init failed: Connection refused
Failed to initialize audio driver 'pulse'
Could not open/initialize audio device -> no sound.
Audio: no sound
Video: no video

Whops …

Fun fact, after some time it might work (if pulseaudio “releases” its lock on the audio device and alsa could be used directly).

AK

No. It is not necessary. It must be played only as one particular user.

Lastly, why would you want to run mplayer as root? Is it somehow critical for your setup? If that’s the case, we might be able to work out a different solution :).

I prefer to run it as normal user, not as root.

You are right. I will remove that option.

And secondly a short look in the crystal ball.

  • pulseaudio running
  • and running as $NORMAL_USER?
  • and blocking audio device for all other users (including root)?

Whops …

Fun fact, after some time it might work (if pulseaudio “releases” its lock on the audio device and alsa could be used).

AK

Do I need pulseaudio? I am doing all this in runlevel 3 in case that matters. I will report the result of the same test you show a little later.

If you need it is something I can not answer, but perhaps you should check if you are using it, ps or pidof are your friends.

A different approach might be to create a custom service to run at startup as described here (using mpv to play sound)…

http://blog.roboticoverlords.org/post/97883292978/how-to-add-a-startup-sound-to-arch-linux

Since you want a regular user to be able to control this, you could create a systemd user service instead…

Checked with ps grepping for pulse - no such process.

Thanks, I will check this too.

A custom service worked perfectly. Thank you!

acer:/usr/lib/systemd/system # cat player.service 
[Unit]
Description=player
After=alsa-restore.target


[Service]
Type=forking
ExecStart=/home/username/bin/playmusic
User=username
Group=users


[Install]
WantedBy=multi-user.target

Glad that it works for you :slight_smile: