title: Running command-line application from PHP as specific user
body: <p>I am running Apache on my localhost. From a PHP script run as www-user I would like to control Rhythmbox playback on my machine. So far I have a simple command in my PHP script:</p>
  
  <p><code>exec('rhythmbox-client --pause');</code></p>
  
  <p>This works great when I run it from the command-line as me, but if it runs as www-user I guess <code>rhythmbox-client</code> doesn't know/can't access my instance of Rhythmbox.</p>
  
  <p>Is there an easy way for that PHP script to run as my user rather than www-user, or to tell <code>rhythmbox-client</code> which instance to control?</p>
  
  <p>The overall application is that when my phone goes off-hook it calls my PHP script which pauses music, and resumes playback when the phone is on-hook. I love VoIP phones!</p>
  
  <p><strong>Solution:</strong>
  Thanks to Carpetsmoker and Tarek I used <code>sudo</code> as the answer but there was a couple of problems. To overcome them I did the following:</p>
  
  <p>Created a bash script to call <code>rhythmbox-client</code>. This bash script was executed using <code>sudo</code> in PHP as described in the answer below. Unfortunately <code>rhythmbox-client</code> didn't know what environment to control, so the bash script looks like this:</p>
  
  <pre><code>#! /bin/bash
  DBUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS /proc/*/environ 2&gt; /dev/null| sed 's/DBUS/\nDBUS/g' | tail -n 1`
  if [ "x$DBUS_ADDRESS" != "x" ]; then
          export $DBUS_ADDRESS
          /usr/bin/rhythmbox-client --pause
  fi
  </code></pre>
  
  <p>Now that bash script can be executed by PHP and wwwuser, and my phone can pause/play my music!</p>