简体   繁体   中英

How can i run Linux background process using PHP? Tried with system(“sudo -u me mplayer /tmp/audio.wav”) but not working

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!

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 .

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 .

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.

ps: 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!

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

Try this

usermod -aG audio www-data 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM