?
Solved

exec

Posted on 2006-05-15
16
Medium Priority
?
1,865 Views
Last Modified: 2013-11-18
Hi I am trying to play files from a website on my local machine speakers like a jukebox. This is it broken down for testing. If I run the script from the command line php play.php it works fine and plays the song. When  I run it via web browser it doesn't play the song.  Everything else works. I am using Fedora Core 5 with detault install of apache and php 5.
Is there something in apache or php i have to change to be able to run it from the browser?
Side note, how can I run a command line script as apache user just to test it as I can't login as apache?
I am running as my local user account from the command line which works.

<?php
// WORKS FINE
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";

$file = 'test.mp3';

// WORKS FINE
if (file_exists($file)) {
   echo "The file $file exists";
} else {
   echo "The file $file does not exist";
}

DOES NOTHING
playLocal($file);

function playLocal($file){
      $mp3player = ("/usr/bin/mpg321");
      $command = "$mp3player ".escapeshellarg(stripslashes($file));
      $pid = shell_exec("nohup $command > /dev/null 2>&1");
}


?>
0
Comment
Question by:onestar
  • 8
  • 6
  • 2
16 Comments
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16687729
well, things could be tested
1- check that safe mode is set to off in php.ini

2- try this trick
Create a file on cgi-bin directory, like this:

#!/usr/bin/php
<?
  echo shell_exec('whoami');
?>

Don't forget to set the file you created the permission to execute it. Hence, call it from browser and you will se that this script will be executed by the shell user and not the user nobody (apache default user if running a PHP script). [from php.net]


have a look at user comments here. You gonna LOVE it

http://www.php.net/manual/en/function.shell-exec.php
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16688784
A better option is to just download the MP3 from the browser and tell your MIME handlers/plugins handle it.
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16689454
i guess onestar is not just trying to play some MP3s, he is playing around with shell and exec, it is pretty cool actually, super power if I may say :)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16689488
Does the webserver user have access to the appropriate devices you are asking it to communicate with?

This is going to be a permissions issue.

Oh!

Just thought.

Does it run for about 30 seconds and then stop?

Take a look at the php.ini file (or equivalent) and look for

max_execution_time = 30     ; Maximum execution time of each script, in seconds


Either set this to 0 to allow no limit or use


<?php
set_time_limit(0); // Allow forever
?>

in your script.

I think you are also going to come up against the issue that PHP will wait until all child processes terminate. This is probably NOT what you want to do.
0
 
LVL 1

Author Comment

by:onestar
ID: 16695553
dr_dedo
1. safe_mode = Off
2. apache

RQuadling
Does the webserver user have access to the appropriate devices you are asking it to communicate with?
How can I check if apache has permission to use the sound card? And if not how to change that?

It doesn't play at all.

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16706620
Good question. I don't use Apache, so I don't know. But I did notice that the program you are running is nohup and that you throw away all the output.

Firstly ...

1 - Try NOT using nohup. Include the set_time_limit(0); and also use error_reporting(E_ALL); Lets see what happens them. Does the program quit instantly or does it last approximately the duration of the track?
2 - Do NOT throw away the responses. Put them to a log file and then read them. Use different logs files for 1, 2, etc. You may simply find that the software cannot find the correct file or something REALLY dumb!

0
 
LVL 1

Author Comment

by:onestar
ID: 16730959
I tried changing apache to run as my normal user account instead of apache user and apache group that I was logged in as and it worked fine.
How can I change the apache user so it has permission to play audio to my computer speakers? I tried exucuting chmod 666 /dev/adsp /dev/adsp1 /dev/audio /dev/audio1 /dev/dsp /dev/dsp1 /dev/mixer /dev/mixer1 /dev/sequencer /dev/sequencer2 and it still didn't work. (I have 2 audio cards thats why i have audio and audio1) and it still didn't work

Thanks Onestar
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 375 total points
ID: 16732326
If you drop the nohup and change the log file output, do the log files reveal anything?

Instead of ...

$pid = shell_exec("nohup $command > /dev/null 2>&1");

try ...

$pid = shell_exec("$command > /dev/log_1 2> /dev_log_2");

And see what log_1 and log_2 have to say for themselves.

Also, as there is no "nohup", the command MAY run for as long as the command dictates (may be a long time).

0
 
LVL 1

Author Comment

by:onestar
ID: 16736206
<?php
error_reporting(E_ALL);
set_time_limit(0);

//  Start TIMER
//  -----------
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];

echo shell_exec('whoami');

$file = 'test.mp3';

if (file_exists($file))
{
         echo "Playing... $file";
      $mp3player = ("/usr/bin/mpg321");
      $command = "$mp3player ".escapeshellarg(stripslashes($file));
      //$pid = shell_exec("nohup $command > /dev/null 2>&1");
      $pid = shell_exec("$command > /tmp/log_1 2> /tmp/dev_log_2");
} else
{
   echo "The file $file does not exist";
}

//  End TIMER
//  ---------
$etimer = explode( ' ', microtime() );
$etimer = $etimer[1] + $etimer[0];
echo '<p style="margin:auto; text-align:center">';
printf( "Script timer: <b>%f</b> seconds.", ($etimer-$stimer) );
echo '</p>';
?>

Browser Output no audio for the length of the song
apache Playing... test.mp3

Script timer: 159.518497 seconds.

log_1 is empty
 cat /tmp/dev_log_2
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.59q (2002/03/23). Written and copyrights by Joe Drew.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!

Playing MPEG stream from test.mp3 ...
MPEG 1.0 layer III, 128 kbit/s, 44100 Hz joint-stereo
ALSA lib confmisc.c:672:(snd_func_card_driver) cannot find card '1'
ALSA lib conf.c:3492:(_snd_config_evaluate) function snd_func_card_driver returned error: No such device
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:3492:(_snd_config_evaluate) function snd_func_concat returned error: No such device
ALSA lib confmisc.c:1072:(snd_func_refer) error evaluating name
ALSA lib conf.c:3492:(_snd_config_evaluate) function snd_func_refer returned error: No such device
ALSA lib conf.c:3961:(snd_config_expand) Evaluate error: No such device
ALSA lib pcm.c:2099:(snd_pcm_open_noupdate) Unknown PCM default
ALSA snd_pcm_open error: No such device

[2:39] Decoding of test.mp3 finished.

From command line plays fine
php play2.php
paul
Playing... test.mp3<p style="margin:auto; text-align:center">Script timer: <b>159.496146</b> seconds.</p>

cat dev_log_2
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.59q (2002/03/23). Written and copyrights by Joe Drew.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!

Playing MPEG stream from test.mp3 ...
MPEG 1.0 layer III, 128 kbit/s, 44100 Hz joint-stereo

[2:39] Decoding of test.mp3 finished.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16742803
A few dumb questions.

1 - Is your PC also the server?
2 - If No to 1, are there speakers connected to the server?
3 - If No to 2, do you run the command line on the server?
4 - If No to 3, how do you expect the server to send the files to your speakers?

From what I can see above, you run the program on YOUR computer using YOUR soundcard, it works.

You run the same code on the server it fails as the server has no sound card or a different one or different drivers/setup/etc.

Surely the best way is to simply have the mp3 file downloaded to the pc and let the browser launch the appropriate player.

If you intend to use a streaming technology, then you need to know what protocols the streaming client will use to receive the data from the server and if it can be initiated by a web command.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16742875
Think about which physical processor is executing the code.

At the command line, no matter where the program physically sits it is running on the computer whose command line you are typing into. This is most likely YOUR computer (the one with the sound card and the speakers).

When Apache launches the same program, the server is running the program on its processor with no sound card or speakers! QED No sound!

We can see that the MP3 output is being handled as the program runs for the approx correct amount of time, but the mp3 player has no where to send the sound.

So, what is the GLOBAL name for your sound card. Does such a thing exist? Say you had another computer on the network. How would you address the other computers sound card relative to YOUR pc.

I come from windows. We have a technique called sharing.

So, computer 1's C:\Shared folder is available as a share called C_SHARE_1 (for example).

On computer 2, I can "map" the share from computer 1 to a point (drive letter or mount point if the OS is uptodate) on MY computer

MAP E: \\computer1\C_CHARE_1

I now have MY C: (disk), D: (DVD) and E: (computer1's shared folder) as drive letters on my computer.

On windows I cannot talk to another computers soundcard as easily. Instead I could stream it data to a port in a protocol which it would have to be servicing.

I think something similar is needed for you, but I don't have a clue about sharing devices across a network for *ix platforms.
0
 
LVL 1

Author Comment

by:onestar
ID: 16743676
The server is the computer I am executing the all the commands on via a shell or apache localhost. I am logged into it locally as my regular user and the sound card output is hooked up right to my stereo.
I want the sound to play through my stereo that is conectted to the server not my other pc's speakers. I have now tried mpd and I got that to work but I still want to know how to get this to work.

I do not want to stream any files I want them to play locally using the servers speakers. I wanted it to run via apache so I could connect to the server from other pc's and play music on my home stereo. There is actually 2 sound cards in the server on board and sound blaster. I can play sound files fine using an mp3 player on the server or from the command line when I am logged in as paul (my regular user). I just seems apache doesn't have permissions to use the sound card. If i change httpd.conf so apahce runs as user paul and group paul the files play fine (messing up other apache things). I read some where when you log in as your regular user it changing permissions of /dev/audio etc. to your local user so you can access the sound card.

I don't know this is fustrating....


Paul
0
 
LVL 1

Author Comment

by:onestar
ID: 16748405
Fixed!

Orginal
$command = "$mp3player ".escapeshellarg(stripslashes($file));

New
$command = "$mp3player -o oss -a /dev/audio1 ".escapeshellarg(stripslashes($file));

Then chmod 606 /dev/audio1 (second sound card) to give apache permission to the device.


0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16749038
Well done! No idea if I helped at all on that one!
0
 
LVL 1

Author Comment

by:onestar
ID: 16750701
Well you got me on the right track with what was in the log file.

Thanks for your time. I gave you the points :)

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16750883
Thank you.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question