Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active August 17, 2023 17:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/66c3231fce811d27bff4c346c1c941de to your computer and use it in GitHub Desktop.
Save code-boxx/66c3231fce811d27bff4c346c1c941de to your computer and use it in GitHub Desktop.
PHP Daemon

SIMPLE PHP DAEMON

https://code-boxx.com/simple-php-daemon/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) COMMAND LINE ONLY
require "cli-only.php";
// (B) SETTINGS
define("CYCLE_PAUSE", 5); // wait 5s between cycles
// (C) RUN - INFINITE LOOP
while (true) {
echo "It works!" . PHP_EOL;
sleep(CYCLE_PAUSE);
}
<?php
// (A) COMMAND LINE ONLY
require "cli-only.php";
// (B) SETTINGS
define("CYCLE_PAUSE", 3600); // wait 1hr (3600s) between cycles
define("DATA_URL", "http://localhost/2b-dummy.txt"); // get dummy data from this url
// (C) RUN - FETCH DATA ON INTERVALS
while (true) {
// (C1) FETCH UPDATES FROM DUMMY SERVER
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, DATA_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// (C2) RESULTS - UPDATE DATABASE
if (curl_errno($ch)) { echo curl_error($ch) . PHP_EOL; }
else {
echo $result . PHP_EOL;
$result = json_decode($result, true);
// $sql = "UPDATE `rates` SET `EUR` = ?";
// $sql = "UPDATE `rates` SET `GBP` = ?";
}
// (C3) NEXT CYCLE
curl_close($ch);
sleep(CYCLE_PAUSE);
}
{"EUR":1.17,"GBP":1.38}
<?php
// (A) COMMAND LINE ONLY
require "cli-only.php";
// (B) SETTINGS
define("CYCLE_PAUSE", 10); // wait 10s between cycles
define("CHECK_URL", "http://localhost"); // url to monitor
define("LOG_FILE", __DIR__ . DIRECTORY_SEPARATOR . "stats.txt"); // log file
// (C) HELPER FUNCTION - ADD LINE TO LOG FILE
function logger ($msg) {
file_put_contents(LOG_FILE, sprintf("[%s] %s\r\n", date("Y-m-d H:i:s"), $msg), FILE_APPEND);
}
// (D) START!
logger("Start monitoring");
while (true) {
// (D1) CURL TEST FETCH
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, CHECK_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// (D2) RESULT
if (curl_errno($ch)) { logger(curl_error($ch)); }
else {
$info = curl_getinfo($ch);
logger($info["http_code"]." - ".$info["total_time_us"]);
}
// (D3) NEXT CYCLE
curl_close($ch);
sleep(CYCLE_PAUSE);
}
<?php
// CREDITS : https://stackoverflow.com/questions/933367/php-how-to-best-determine-if-the-current-invocation-is-from-cli-or-web-server
function is_cli () {
if (php_sapi_name()==="cli") { return true; }
if (defined("STDIN")) { return true; }
if (array_key_exists("SHELL", $_ENV)) { return true; }
if (!array_key_exists("REQUEST_METHOD", $_SERVER)) { return true; }
if (empty($_SERVER["REMOTE_ADDR"]) && !isset($_SERVER["HTTP_USER_AGENT"]) && count($_SERVER["argv"])>0) { return true; }
return false;
}
if (!is_cli()) { exit("Please run this in the command line."); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment