3

I want to refresh the browser, like pressing the F5 or Ctrl+F5 key on the keyboard with a shell script.

I´m new in writing shell scripts so i aks you to help me with this:

when I type in this into the command line, the refresh works perfectly:

DISPLAY=:0 xdotool getactivewindow key F5

I wrote a script refresh.sh with this content:

export DISPLAY=:0.0
xdotool getactivewindow
xdotool key F5

But it reurns:

pi@babaloo ~ $ sudo sh /var/www/refresh.sh
No protocol specified
Error: Can't open display: (null)
Failed creating new xdo instance
No protocol specified
Error: Can't open display: (null)
Failed creating new xdo instance

can someone give me a hint?

Chromium Browser is in Kiosk mode, here is my

/etc/xdg/lxsession/LXDE/autostart

-

#@lxpanel --profile LXDE
#@pcmanfm --desktop --profile LXDE
#@xscreensaver -no-splash
@xset s off
@xset -dpms
@xset s noblank
@chromium --kiosk --incognito http://localhost/output/output.php?monitor=1

~~ EDIT:

I changed my script to

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

when i start it from console with

sh /var/www/refresh.sh

it works!

But now i want to execute this script by PHP with user www-data. Therefor i added to visudo:

www-data ALL=(ALL) NOPASSWD: ALL
www-data ALL=(ALL) NOPASSWD: /var/www/refresh.sh

and Created a PHP-File with this content:

<?php
exec("sudo sh /var/www/refresh.sh");
?>
<h1>Refreshing output</h1>

But nothing happend...

Is there a chance to get more informations about the returning errors? I found nothing in documentation for "exec"-command.

Also tried:

<?php
$output = array();
$output[] = exec("sudo sh /var/www/refresh.sh");
print_r($output)
?>

but the returning array is empty

s3bi
  • 160
  • 1
  • 2
  • 7

3 Answers3

1

I used a mix with PHP and jQuery Ajax now. I write a parameter into a Databas, if its "0", nothing will happen, if it is "1" the page relaods.

   var times = 0;

    var refreshId = window.setInterval(function(){
      // check refresh!
      $.ajax({
        type : "GET",
        url : "check.php",
        cache : false,
        success: function(data){
          var data = $.parseJSON(data);
          var refresh = data.refresh;

          if(refresh == 1){
            location.reload();
          }
...
s3bi
  • 160
  • 1
  • 2
  • 7
1

I achieve to do this by creating a dummy file on the folder, and making the php check if that file exists.

1.- My website has a time interval to check it ever

$(document).ready(function() {
      setInterval(checkRefresh, 10000);    
 });

function checkRefresh()
{
    $.ajax({
            url: "checkrefresh.php"
    }).done(function (response) {
        if (response == 1)
        {
          document.location.reload(true);          
        }
    });
}

2.- In an administrative website, I have a button to launch the php that creates the file:

(in admin.html..)

  $('#btn_refresh').click(function () {        
          $.ajax({
              url: "refresh.php"
          }).done(function (msg){
              alert(msg);
          }).fail(function () {
              alert('Error calling refresh script!');
          });
     });

(refresh.php contents...)

<?php     
    // I put the time inside the file, to help me know when something didn't work ok if the file gets stuck there
    $time = time();

    if(!touch('refreshcookie', $time)) {
        echo 'Something went wrong when trying to reload the webpage!';
    } else {
        echo 'Refresh order sent, this may take up to ten seconds';
    }
?>

3.- Finally, the checkrefresh.php checks if the file is there, and if is so responds and deletes it

<?php

    $imagefile = 'admin/refreshcookie';
    if (file_exists($imagefile))
    {
        unlink($imagefile);
        echo 1;
    }
    else
    {
        echo 0;
    }
?>

I would prefer to have an event instead of checking every x seconds, but it works...

Hope it helps!

0

Try this:

<?php
@exec("sudo sh /var/www/refresh.sh", $output, $resultCode);
echo "<br>output: ";
var_dump($output);
echo "<br>resultCode: ";
var_dump($resultCode);
// remove the echoes/dumps later
?>

Something similar is working on my pi (3B+ => stretch)

Chiwda
  • 183
  • 2
  • 14