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

  • 1
    It might be because you run your script as root. – kursus Nov 6 '13 at 18:46
  • theres no difference when I run it without sudo – s3bi Nov 7 '13 at 12:04
  • Why do you run PHP exec to refresh a web page. That is so overkill! Just use Javascript to refresh the page window.location.reload(false); -You use a timer, or ajax to control when to refresh. Since you have PHP running to execute the page you are viewing I assume the page you want to refresh is your page... – ppumkin Nov 7 '13 at 13:18
  • I have a lot of JS stuff in the browser and I need a "real" browser refresh (without javascript). This should clear the DOM. Another posibility is to close the browser with killall chromium and restart the application after... but i thin it´s a more "overkill". – s3bi Nov 7 '13 at 13:22
  • meta refresh tag ? – Lawrence Nov 7 '13 at 13:26

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();
          }
...
  • use location.reload(true) to force the cache to be dropped and load the freshest data. like CRTL+F5. You should mark your answer as solved. – ppumkin Nov 7 '13 at 15:15
  • 1
    thanks, I already solved the caching problem in the document header, but your solution is also good! – s3bi Nov 7 '13 at 17:42

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!

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.