Search code examples
phpshellraspberry-pi2xdotool

Run Xdotool on Rasperry PI as user "www-data"


I'm using xdotool on rasperry pi to refresh iceaweasel browser. Now, I want to reload the webpage after the server in LAN is booted.

For this I write a PHP script on Raspberry (refresh.php):

<?php
 exec("sh /var/www/refresh.sh");
?>

resfresh.sh

export DISPLAY=:"0.0"
XAUTHORITY=/home/pi/.Xauthority
xdotool getactivewindow
xdotool key F5

If I start the PHP script from console, the refresh is working! If I start the PHP from external browser, the refresh not working!

That's the Apache error-log:

No protocol specified
Error: Can't open display: (null)
Failed creating new xdo instance  

I think, there's a problem with xdotool and the user www-data.

Could anybody help me?


Solution

  • Add www-data permission to execute system tools

    DISPLAY=:0 xhost + local:www-data

    On your script /refresh.sh

    #!/bin/bash
    export DISPLAY=:0 && xdotool key F5
    

    On your php file

    <?php
    shell_exec("/refresh.sh " . $phone);
    ?>
    

    then add executable permission to your php file

    sudo chmod +x refresh.php
    

    Execute the php file via URL and your good to go.