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

asked Nov 6, 2013 at 17:02
8
  • 1
    It might be because you run your script as root. Commented Nov 6, 2013 at 18:46
  • theres no difference when I run it without sudo Commented Nov 7, 2013 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... Commented Nov 7, 2013 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". Commented Nov 7, 2013 at 13:22
  • meta refresh tag ? Commented Nov 7, 2013 at 13:26

3 Answers 3

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();
 }
...
answered Nov 7, 2013 at 14:12
2
  • 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. Commented Nov 7, 2013 at 15:15
  • 1
    thanks, I already solved the caching problem in the document header, but your solution is also good! Commented Nov 7, 2013 at 17:42
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!

answered Dec 2, 2015 at 23:12
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)

answered Sep 2, 2023 at 13:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.