1

I would like to run a php script every 5 seconds.

That's what I've done :

(essay.sh file)

#!/bin/bash

while :
do
    php a_file.php
    sleep 5
done

(the php file in a nutshell)

<?php
// sql call

// sql into $json

$file = 'json.txt';

file_put_contents($file, $json);
?>

There is no error when I do: ./essay.sh, but the json.txt is not refreshed (nothing happen, and I've set the rights to 0755 for the both files).

When I use the direct url of a_file.php in my browser the script is working, and a new json.txt is generated.

Thank you for your help !

Alex01
  • 330
  • 3
  • 14
  • I don't think running script every 5 mins can be done except using cronjob. – Disha V. Jul 28 '15 at 05:25
  • Hi, I'd like to do it every 5 seconds and cron is limited to 1 minute at least – Alex01 Jul 28 '15 at 05:30
  • http://stackoverflow.com/questions/27898231/run-php-script-every-5-seconds – Disha V. Jul 28 '15 at 05:34
  • You could create a [daemon in PHP](http://stackoverflow.com/questions/2036654/run-php-script-as-daemon-process) that basically runs forever once start (either through cron or at system start). Allow the daemon to do some work and then sleep for 5 seconds. You could even fork processes depending on your situation. – zedfoxus Jul 28 '15 at 05:44
  • What are you trying to do, exactly? i.e., why does it need to run every 5 seconds? There may be a more efficient solution to your problem. – Trick Jul 28 '15 at 09:35

2 Answers2

0

Because your are setting 0755 you need to run your script as the same user as the owner of the json file.

Try to set the permissions to 775 or run the script as the owner of that json file.

m.zander
  • 513
  • 4
  • 10
0

I have tried your script in my pc and found out it works fine. This is the file privilege i set: enter image description here

cedricliang
  • 352
  • 1
  • 7