4

When i execute following none works (checked with ps aux | grep mplayer, where 1.wav is a 10 minutes audio file):

system("mplayer /tmp/1.wav"); // failed
system("sudo -u myusername mplayer /tmp/1.wav"); // failed
system("mplayer /tmp/1.wav &"); // failed
system("(mplayer /tmp/demo.wav) >/dev/null &"); //failed according to: http://www.php.net/manual/fr/function.system.php#88543
system("sudo -u myusername -i mplayer /tmp/demo.wav");

How can i run a background process with PHP? Only this works but its like batch file and i am in the same system. $ php -r "system('mplayer /tmp/demo.wav');";

Thank you

Note: 1) error:

Cannot find HOME directory.
Home directory /var/www not ours.
AO: [pulse] Init failed: Connection refused
Failed to initialize audio driver 'pulse'
Home directory /var/www not ours.
waitpid(): No child processes
[AO_ALSA] alsa-lib: pulse.c:229:(pulse_connect) PulseAudio: Unable to connect: Internal error

[AO_ALSA] Playback open error: Connection refused
Failed to initialize audio driver 'alsa'
[AO OSS] audio_setup: Can't open audio device /dev/dsp: No such file or directory
Home directory /var/www not ours.
waitpid(): No child processes
[AO_ALSA] alsa-lib: pulse.c:229:(pulse_connect) PulseAudio: Unable to connect: Internal error

[AO_ALSA] Playback open error: Connection refused
Home directory /var/www not ours.
AO: [pulse] Init failed: Connection refused
Home directory /var/www not ours.
waitpid(): No child processes
Home directory /var/www not ours.
[AO_ALSA] alsa-lib: pulse.c:229:(pulse_connect) PulseAudio: Unable to connect: Connection refused

Aborting. $HOME not set!

2) & 5) error:

sudo: sorry, you must have a tty to run sudo

3) error:

Cannot find HOME directory.
File not found: '/tmp/1.wav'
Failed to open /tmp/1.wav.

4) error:

Cannot find HOME directory.
Home directory /var/www not ours.
AO: [pulse] Init failed: Connection refused
Failed to initialize audio driver 'pulse'
Home directory /var/www not ours.
waitpid(): No child processes
[AO_ALSA] alsa-lib: pulse.c:229:(pulse_connect) PulseAudio: Unable to connect: Internal error

[AO_ALSA] Playback open error: Connection refused
Failed to initialize audio driver 'alsa'
[AO OSS] audio_setup: Can't open audio device /dev/dsp: No such file or directory
Home directory /var/www not ours.
waitpid(): No child processes
[AO_ALSA] alsa-lib: pulse.c:229:(pulse_connect) PulseAudio: Unable to connect: Internal error

[AO_ALSA] Playback open error: Connection refused
Home directory /var/www not ours.
waitpid(): No child processes
AO: [pulse] Init failed: Internal error
Home directory /var/www not ours.
waitpid(): No child processes
Home directory /var/www not ours.
waitpid(): No child processes
[AO_ALSA] alsa-lib: pulse.c:229:(pulse_connect) PulseAudio: Unable to connect: Internal error

Aborting. $HOME not set!
  • What was the [error return from `system()`](http://php.net/manual/en/function.system.php)? – sarnold Jun 05 '11 at 22:40
  • 1
    `sudo -u username mplayer foo` will require adding an entry to your `sudoers(5)` file to allow whatever user account is running your `php` interpreter to change to your user account without a password or other authentication. Since it would allow _any_ `php` code to execute code as _you_, it is probably a horrible idea. What are you _really_ trying to accomplish? – sarnold Jun 05 '11 at 22:43
  • @sarnold: second command gives : "sudo: sorry, you must have a tty to run sudo " –  Jun 05 '11 at 22:43
  • 2
    @89899.3K, ha! Those clever `sudo(8)` developers. Nice catch on their part. – sarnold Jun 05 '11 at 22:44
  • @sarnold: please see above updates with 1,2,3 all the errors. –  Jun 05 '11 at 22:47
  • @89899.3K - What happens if you use the `-i` option to sudo? E.g. `sudo -u myusername -i mplayer /tmp/1.wav` – Joe Kington Jun 05 '11 at 22:57
  • @Joe Kington: please see above error 5 same as others. –  Jun 05 '11 at 23:09

5 Answers5

1

Apparently, there is no simple way to do so: neither the system() function or back-quotes notation allow you to run background tasks… Someone posted a (quite cumbersome) solution to this on the PHP doc website.

Thomas Hupkens
  • 1,570
  • 10
  • 16
  • There is also this solution: http://www.php.net/manual/fr/function.system.php#59884 – Thomas Hupkens Jun 05 '11 at 22:53
  • unfortunately windows only i presume the link that you mentioned? ex: new COM("WScript.Shell"); –  Jun 05 '11 at 22:57
  • tried your last reference, but i have still same issue. please see above error 4. –  Jun 05 '11 at 23:01
1

You can use this with Linux configuration of sudoer. And then it works without password prompt.

0

Looks like mplayer is trying to use the pulseaudio framework for audio output, but can't connect to a pulseaudio daemon, because none is running. Maybe -ao alsa or -ao oss can bypass pulseaudio and still work.

If not, maybe you need to run pulseaudio as a system-wide daemon (described in the pulseaudio(1) manpage, sounds complicated).

Or, maybe you should run mplayer as your user account in the first place, use the -input file=<foo> command line option, create a fifo (see mkfifo(1) for details, but in general mkfifo /tmp/mplayer_control_pipe) and change permissions to allow both your user account and the user account running the php code to read and write the fifo. Then your php script can simply echo commands into the pipe, and your already running mplayer instance will play the specified files.

This way, your php interpreter won't be stuck holding filedescriptors open while a wav file is playing -- if you're running this under the control of a web server, that'd probably keep the web browser from ever rendering any output, until mplayer dies.

Community
  • 1
  • 1
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • FYI, vim /tmp/test.php; ; php /tmp/test.php; works without any error –  Jun 05 '11 at 23:14
  • @898, that's because _you_ are executing it there, but if (as I assume but don't know for certain) you're executing your `php` interpreter through a web server or FastCGI tool, _it runs as a different user_. And thus, cannot connect to _your_ `pulseaudio` daemon. – sarnold Jun 05 '11 at 23:16
  • you are right. Thats why i wanted to do $ php -r "system('sudo -u MyUsernameWhoHavePermission mplayer /tmp/demo.wav');"; –  Jun 05 '11 at 23:24
  • @898, yeah, and I don't think that's going to work, which is why I suggested running `mplayer` in command-mode (`-input file=/path/to/control/pipe`) and _controlling_ it from `php`. – sarnold Jun 05 '11 at 23:26
0

create a script file

/home/www-data/php_user.sh

#!/bin/bash
whoami
read x

chgrp www-data /home/www-user/php_user.sh
chmod +x /home/www-user/php_user.sh

from the php script

system( "/home/www-user/php_user.sh" );

the goal is to print the web 'user' account used by the web server framework.

is it www-data ? now issue

adduser www-data

and try to login to that user and run mplayer :

sudo su www-data
mplayer /tmp/1.wav

if it works interactive, then retry from php

system( "mplayer etc..." );

I have not tried it. However the basic idea is : test everything by hand, step by step; then built-in php.

p.s.: if you want / have to use a account different than www-data, just create this user, try to login and run mplayer.

if ok, then in your web server framework ( apache ( ? ) and php ) set the user to this one.

ciao!

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Massimo
  • 3,171
  • 3
  • 28
  • 41
  • FYI, this works the way you were trying to find. $ php -r "system('mplayer /tmp/demo.wav');"; –  Jun 05 '11 at 23:22
-1

Try this

usermod -aG audio www-data 
Muhammad
  • 3,169
  • 5
  • 41
  • 70